Python Forum

Full Version: Problem defining a variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def checkUserDesiredMethod():
    userDesiredMethod=input("Would you like to register an account or login?\n(reg) /\n(login) /\n(none) /\n").lower()
    if userDesiredMethod=="reg":
        userWantsToRegister=True
    elif userDesiredMethod=="login":
        userWantsToLogin=True
    elif userDesiredMethod=="none":
        pass
    else:
        errorType("invalidInput")
def performUserDesiredMethod():
    if userWantsToRegister:
        regAcc()
    elif userWantsToLogin:
        loginMethod()
    else:
        print("Error")
So here is part of my password system in python. Don't worry, I have defined regAcc(), loginMethod() and errorType() and as far as I am aware, they are not what is causing the issue. But later on in the script I write this:
checkUserDesiredMethod()
performUserDesiredMethod()
and when I run the script, after I enter something for the input, there is an error and it says
Error:
Traceback (most recent call last): File "C:\Users\Student\Python\passwordSystem.py", line 108, in <module> performUserDesiredMethod() File "C:\Users\Student\Python\passwordSystem.py", line 96, in performUserDesiredMethod if userWantsToRegister==True: NameError: name 'userWantsToRegister' is not defined
I'm not sure why it says that userWantsToRegister is not defined as it is defined in the subroutine checkUserDesiredMethod() as a boolean data type / boolean variable (if necessary), for example if I enter 'reg' for the input for the ' userDesiredMethod=input("Would you like to register an account or login?\n(reg) /\n(login) /\n(none) /\n").lower() ' part of the checkUserDesiredMethod function, then userWantsToRegister is assigned 'True' but if I input anything other than 'reg' then userWantsToRegister is not defined or assigned anything, but apparently, even if I enter 'reg', userWantsToRegister is not defined anyway, even though I have entered 'reg' which is supposed to fulfill the requirement for the the 'if' statement in the checkUserDesiredMethod() function which leads to userWantsToRegister being assigned 'True':
(if userDesiredMethod=="reg":
userWantsToRegister=True).
Do you know what is causing the error and how I can make the program proceed to performing the subroutine regAcc() if userWantsToRegister==True, or loginMethod() if userWantsToLogin==True? (and so basically just doing what performUserDesiredMethod() is supposed to do?)
The problem is userWantsToRegister and userWantsToLogin is local attribut from CheckuserDesiredMethod().

Maybe you can built a class ? or check this:
def checkUserDesiredMethod():
    userDesiredMethod=input("Would you like to register an account or login?\n(reg) /\n(login) /\n(none) /\n").lower()
    if userDesiredMethod=="reg":
        userWantsToRegister=True
    elif userDesiredMethod=="login":
        userWantsToLogin=True
    elif userDesiredMethod=="none":
        pass
    else:
        Exception("invalidInput")
def performUserDesiredMethod():
    if checkUserDesiredMethod.userWantsToRegister:
        regAcc()
    elif checkUserDesiredMethod.userWantsToLogin:
        loginMethod()
    else:
        print("Error")
BamBi25, you solution doesn't work. You'll get an attribute error on line 12. You need to return values from the check and pass them to the perform:

def checkUserDesiredMethod():
    userDesiredMethod=input("Would you like to register an account or login?\n(reg) /\n(login) /\n(none) /\n").lower()
    return userDesiredMethod

def performUserDesiredMethod(method):
    if method == 'reg':
        regAcc()
    elif method = 'login':
        loginMethod()
    else:
        print('Error')

user_choice = checkUserDesiredMethod()
performUserDesiredMethod(user_choice)
Parameters are return values are how you pass information between functions. You need to become familiar with them, and you need to understand that variables defined in a function are not visible outside of that method.
Ah, so is it impossible to define variables with a def function that are recognisable outside of the method?
(Dec-16-2019, 06:27 PM)rix Wrote: [ -> ]Ah, so is it impossible to define variables with a def function that are recognisable outside of the method?

It is possible, but it is a bad idea. Doing so makes it harder to track what is going on in your program. This makes it harder to debug and maintain, even if you get it working to start with. It is much better to either pass values around with parameters and return statements, or to use classes and maintain a state with instance attributes.
(Dec-16-2019, 06:27 PM)rix Wrote: [ -> ]Ah, so is it impossible to define variables with a def function that are recognisable outside of the method?

Trust me, some time down the road you will change something somewhere and have "undefined" errors for days.

The solution is get things you need back from functions using "return" statements.
def double(x):
    x = x * 2
    return x

x = 5
print(x)
x = double(x)
print(x)
Ah. Idea thank you all. I'll keep this thread unsolved until I have rectified the errors in my script so that if I have any more troubles I won't need to create a new thread / set this one 'unsolved', but thank you for your help!