0

我的问题是,我在此脚本中不断收到未定义的全局名称错误:

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()

我的回溯给了我这个:

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

我究竟做错了什么?我想将用户名导入到我创建的 getUsername() 函数中。请记住,我是脚本新手,我只是想在自己的时间学习,这只是一个测试脚本。任何建议都会很棒。

4

2 回答 2

1

您正在使用称为参数getUsername的变量调用该函数,该变量username当时未定义。事实上,它是在getUsername函数中定义的,但也不username是。相反,最终成为包含真实用户名的变量的是confirmUser. 在这种情况下这种情况是有道理的,但username应该包含有效/注册的用户名。反过来,这实际上应该是一种包含多个用户名的列表,专用函数可能是一种可行的方法。

有趣的是:你一开始就用 regLog-thing 做对了。在那里,您通过输入定义 regLog,然后检查其值。

您绝对应该再看一下有关变量、函数(及其参数)的文档,以及一般的基础知识。哦,它应该raw_input()代替input(). 至少在这种情况下。

这将是我快速而肮脏(且不完整)的方法:

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()
于 2013-09-19T00:09:32.800 回答
0

您在拨打电话时尚未初始化用户名loggingIn()-getUsername(username)您要验证哪个用户名,它来自哪里?鉴于它找不到本地username,它在全局命名空间中查找并且无法在那里找到它 - 因此出现错误。

于 2013-09-18T23:57:55.743 回答