Home Using variables from separate functions in Python?
Reply: 2

Using variables from separate functions in Python?

lucy150602
1#
lucy150602 Published in 2017-11-12 15:56:47Z

I am currently working on a coding project where I have set several functions. I am trying to figure out how to use variables from different function. I have tried using 'self' but it hasn't worked. Can anyone help me sort out my code?

class A(object):

    def intro1(self):
        print("Welcome to the XXCI forum!")
        self.intro2()

    def intro2(self):
        print("Press 'L' to log in and 'S to sign up.")
        ch1 = input()
        if ch1 == 'l' or 'L':
        self.log_in()
        elif ch1 == 'S' or 's':
        self.sign_up1()
        else:
        print("Your input was invalid")
        self.intro2()

    def sign_up1(self):
        print("Please enter your first name:")
        fn1 = input()
        if len(fn1) >= 3:
            self.sign_up2()
        elif len(fn1) <3:
            print("Please enter a name that is equal to or over three characters!")
            self.sign_up1()

    def sign_up2(self):
        print("Please enter your last name.")
        ln1 = input()
        if ln1 == ln1:
            with open(ln1.txt, "a") as ln1:
                ln1.write("Age: " + ag1 + ".")
                sign_up3()

    def sign_up3():
        print("Please enter your age.")
        ag1 = input()
        if ag1 > 90:
            print("Please enter an age under 90 and equal to 16 or over.")
        elif ag1 <16:
            print("Please enter an age under 90 and equal to 16 or over.")
        else:
            user_g()

        def user_g():
            username = (fn1[3], str[ag1])
            username.join''
            print("Here is your username:")
            print(username)
            p_creator()

It's obviously not finished yet, but I would really appreciate guidance with how to correct my errors and use variables in different functions. Thank you very much!!

Serg
2#
Serg Reply to 2017-11-12 16:40:37Z

you can find everything in the Python documentation: https://docs.python.org/3/tutorial/classes.html?highlight=class%20attributes%20access#class-objects So you need just create and then call class's attributes.

Also you could use the best python practice DRY (don't repeat yourself) and merge two 'if' statements in def sign_up3 like this: 'if ag1 > 90 and ag1 < 16' (another and shorter way is 'if 16 < ag1 < 90')

Hope it helped anyhow. best regards and good luck.

P.S.: please, use the indentations.

Pal
3#
Pal Reply to 2017-11-12 16:19:20Z

You can use global variable. Assign the variable outside the function. Then inside the function called the variable with global in front of it. For example:

def f():
    global s
    print(s)

s = "Demo of global variable" 
f()
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.254814 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO