如何调试Python代码中的名称错误

我正在尝试制作一个登录系统,但阻止我的是我无法调试的名称错误


know = input("Do you want to signup or log-in \n for signup  enter (s) \n for log-in enter (l):")

if know == "s":

    user_signup = input("Enter username:")

    user_signup2 = input("Enter password: ")

    user_signup3 = int(input("Enetr Age:"))

theinfo = [user_signup , user_signup2,user_signup3]


if know =="l":

    login = input("Enter username: ")

    user_login2 = input("Enter password:")

elif login in theinfo and user_login2 in theinfo:

    print("Log in seccefull")

elif login not in theinfo or user_login2 not in theinfo:

    print("username or password incorect")

这是我发现的错误:


Traceback (most recent call last):

  File "E:\code\gg\.vscode\login.py", line 11, in <module>

    elif login in theinfo and user_login2 in theinfo:

NameError: name 'login' is not defined


繁星coding
浏览 127回答 2
2回答

慕标5832272

know = input("Do you want to signup or log-in \n For signup&nbsp; enter (s) \n For log-in enter (l):")if know == "s":&nbsp; &nbsp; user_signup = input("Enter username:")&nbsp; &nbsp; user_signup2 = input("Enter password: ")&nbsp; &nbsp; user_signup3 = int(input("Enter Age:"))theinfo = [user_signup, user_signup2, user_signup3]print()know = input("Do you want to signup or log-in \n For signup&nbsp; enter (s) \n For log-in enter (l):")print()if know == "l":&nbsp; &nbsp; login = input("Enter username: ")&nbsp; &nbsp; user_login2 = input("Enter password:")&nbsp; &nbsp; if login in theinfo and user_login2 in theinfo:&nbsp; &nbsp; &nbsp; &nbsp; print("Log in successful")&nbsp; &nbsp; elif login not in theinfo or user_login2 not in theinfo:&nbsp; &nbsp; &nbsp; &nbsp; print("username or password incorrect")NameError意味着该变量之前没有定义。您为程序设置条件的方式是错误的。我已经修正了程序。

斯蒂芬大帝

使用 if / elif,将读取零个或一个代码段。让我们看看你的 if / elif / elif 代码:if know =="l":&nbsp; &nbsp; login = input("Enter username: ")&nbsp; &nbsp; user_login2 = input("Enter password:")elif login in theinfo and user_login2 in theinfo:&nbsp; &nbsp; print("Log in seccefull")elif login not in theinfo or user_login2 not in theinfo:&nbsp; &nbsp; print("username or password incorect")如果用户输入“l”,则设置登录。但是,接下来的两个“elif”部分将被跳过。你可以这样解决这个问题:if know =="l":&nbsp; &nbsp; login = input("Enter username: ")&nbsp; &nbsp; user_login2 = input("Enter password:")&nbsp; &nbsp; if login in theinfo and user_login2 in theinfo:&nbsp; &nbsp; &nbsp; &nbsp; print("Log in seccefull")&nbsp; &nbsp; elif login not in theinfo or user_login2 not in theinfo:&nbsp; &nbsp; &nbsp; &nbsp; print("username or password incorect")那么,这就回答了问题。然而,由于多种原因,该代码仍然无法按预期工作。但是,您主要缺少一个用于存储完整用户集的凭据列表。您没有现有用户的凭据,因此无法检查返回用户的用户名和密码。您也不能将新用户的凭据保存到现有用户列表中。know = input('Do you want to signup or log-in \n for signup, enter (s) \n for log-in, enter (l): ').upper()# Start with a fetch of existing users.# I am hard-coding users to keep the example simple.&nbsp;&nbsp;# Don't do this in your actual code.&nbsp; You can use something# like a simple file (though you should not store clear text# text passwords in a text file.&nbsp;# https://www.w3schools.com/python/python_file_handling.aspcredentials = [['Phil', 'my_Password', 37], ['jose', 'espanol', 19]]the_info = []if know == 'S':&nbsp; &nbsp; user_signup = input('Enter username:')&nbsp; &nbsp; user_signup2 = input('Enter password: ')&nbsp; &nbsp; user_signup3 = int(input('Enter age:'))&nbsp; &nbsp; the_info = [user_signup , user_signup2, user_signup3]&nbsp; &nbsp; # The next line save the new user in memory.&nbsp;&nbsp;&nbsp; &nbsp; # Add code to save the info to a file or database.&nbsp;&nbsp; &nbsp; credentials += the_info&nbsp; &nbsp; print('Thanks for signing up.')elif know =='L':&nbsp; &nbsp; login = input('Enter username: ')&nbsp; &nbsp; user_login2 = input('Enter password: ')&nbsp; &nbsp; success = False&nbsp; &nbsp; for credential in credentials:&nbsp; &nbsp; &nbsp; &nbsp; if login.upper() == credential[0].upper() and user_login2 == credential[1]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the_info = credential&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; if len(the_info) > 0 :&nbsp; &nbsp; &nbsp; &nbsp; print(f'Log in successful.&nbsp; Hello {the_info[0]}')&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print('username or password incorrect.')print(f'User info: {the_info}')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python