简体   繁体   中英

I keep getting Global name 'username' not defined?

My issue us that I keep getting a global name not defined error in this script:

import time
def cls():
    print(("\n")*100)
#Registers the Username and Password for the user.
def registerPro():
    cls()
    username=str(input("Enter your Username here: "))
    password=str(input("Enter your Password here: "))
    confirm=str(input("Please confirm your Password: "))
    if password==confirm:
        cls()
        print("Thank you for registering at Apex Industries!")
        time.sleep(3)
        loggingIn()
    else:
        cls()
        print("Your passwords do not match, please re-register.")
        time.sleep(3)
        registerPro()
    return username, password
#Is called by getUsername. If the user entered the password correctly then the login is successful.
def getPass(password):
    cls()
    confirmPass=str(input("Enter your password: "))
    if confirmPass==password:
        cls()
        print("You have logged in successfully.")
    else:
        cls()
        print("Wrong Password. Try again")
        getPass()
#Makes sure the username is in the database.
def getUsername(username):
    cls()
    confirmUser=str(input("Enter your username: "))
    if confirmUser==username:
        getPass(password)
    else:
        cls()
        print("No username with that name in the database.")
        time.sleep(3)
        loggingIn()
#The login for the main program.
def loggingIn():
    cls()
    print("Hello and welcome to the Apex Industries login/register page.")
    print("      Please choose to either Login or Register.")
    print()
    regLog=str(input("Type Login or Register for your choice: "))
    if regLog=='Login':
        getUsername(username)
    elif regLog=='Register':
        registerPro()
    else:
        cls()
        print("That is not a valid option. Try again.")
        time.sleep(3)
        loggingIn()

loggingIn()

My traceback gives me this:

Traceback (most recent call last):
  File "C:/Users/Evu/Desktop/Python/testforLogin", line 59, in <module>
    loggingIn()
  File "C:/Users/Evu/Desktop/Python/testforLogin", line 13, in loggingIn
    registerPro()
  File "C:/Users/Evu/Desktop/Python/testforLogin", line 29, in registerPro
    loggingIn()
  File "C:/Users/Evu/Desktop/Python/testforLogin", line 11, in loggingIn
    getUsername(username)
NameError: global name 'username' is not defined

What am I doing wrong? I want to import username into the getUsername() function I have created. Keep in mind that I am new to scripting and am just trying to learn this on my own time, this is just a test script. Any advice would be awesome.

You are calling the getUsername function with a variable called username as an argument, which is not defined at that point. In fact, it gets defined within the getUsername function, but not as username either. Instead what ends up being the variable containing the real username is confirmUser . That kind of makes sense in that case, but then username should contain the username which is valid / registered. That in turn actually should be some kind of list containing more than just one username, for which a dedicated function would be a possible way to go.

The funny thing: you did it correctly in the beginning with the regLog-thing. There you're defining regLog via input, and afterwards checking its value.

You definitely should have a second look on the documentation regarding variables, functions (and their arguments), the basics in general. Oh, and it should be raw_input() instead of input() . In this case at least.

This would be my quick and dirty (and incomplete) approach:

registeredusers = ['john.doe', 'peter.parker', 'bruce.banner']

def getUsername():
    global registeredusers
    loginname = str(raw_input("Enter username: "))
    if loginname in registeredusers:
        return True
    else:
        return False

def loggingIn():
    regLog = str(raw_input("Login or Register? "))
    if regLog == 'Login':
        if getUsername():
            print "Yay! Logged in"
        else:
            print "Login failed"
    elif regLog == 'Register':
        # do stuff
        pass
    else:
        # do stuff
        pass

loggingIn()

You haven't initialised username when you make the call in loggingIn() to getUsername(username) - which username are you trying to validate, where is it coming from? Given it can't find a local username it looks in the global namespace and can't find it there - hence the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM