-2

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!!

wwii
  • 23,232
  • 7
  • 37
  • 77
  • 1
    This shouldn't even be a class, nor use recursion to implement simple iteration. This entire thing should be rewritten as a *single* function. – chepner Nov 12 '17 at 16:03
  • BTW, `ch1 == 'l' or 'L'` doesn't do what you think it does. It's equivalent to `(ch1 == 'l') or 'L'`. See http://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true and http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values – PM 2Ring Nov 12 '17 at 16:04
  • Welcome to SO. Unfortunately this isn't a discussion forum, or tutorial. Please take the time to read [ask] and the links it contains. You should spend some time working your way through [the Tutorial](https://docs.python.org/3/tutorial/index.html), practicing the examples. It will give you an introduction to the tools Python has to offer and you may even start to get ideas for solving your problem. – wwii Nov 12 '17 at 16:06
  • Also, why do `elif len(fn1) <3`. You just tested `if len(fn1) >= 3`, so if that's false then `len(fn1) <3` _must_ be true. And `username.join''` is wrong, I suspect you meant `''.join(username)`. – PM 2Ring Nov 12 '17 at 16:07
  • The way to use variables from one function in another function is to _pass all the data you need as function arguments_. It's good practice to keep functions pure and avoid side effects. – Håken Lid Nov 12 '17 at 18:35

2 Answers2

0

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()
Pal
  • 920
  • 6
  • 6
0

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.

Serg
  • 16
  • 2