Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem defining a variable
#1
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?)
Reply
#2
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")
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Ah, so is it impossible to define variables with a def function that are recognisable outside of the method?
Reply
#5
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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)
Reply
#7
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dynamic variable name declaration in OOP style project problem jacksfrustration 3 718 Oct-22-2023, 10:05 PM
Last Post: deanhystad
  Defining an object's argument whose name is stored in a variable arbiel 2 2,146 Dec-11-2020, 10:19 PM
Last Post: arbiel
  Reset Variable problem IcodeUser8 3 2,312 Jul-20-2020, 12:20 PM
Last Post: IcodeUser8
  Variable from notepad problem samuelbachorik 2 2,260 Apr-10-2020, 09:04 AM
Last Post: samuelbachorik
  Problem: Get variable JohnnyCoffee 0 1,571 Feb-22-2020, 09:26 PM
Last Post: JohnnyCoffee
  Problem writing a variable value to Excel Chuck_Norwich 1 1,903 Jul-25-2019, 02:20 PM
Last Post: Chuck_Norwich
  A problem with defining variables meru120 5 3,340 Apr-16-2019, 03:13 PM
Last Post: buran
  Pass variable script return twice output problem Faruk 8 4,291 Dec-26-2018, 11:57 AM
Last Post: Faruk
  Problem with defining a function snow_y 4 3,153 Nov-26-2018, 02:13 AM
Last Post: snippsat
  Problem with variable O_Pas_Sage 7 4,410 Aug-25-2018, 04:07 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020