为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁星coding 2023-10-25 10:49:58
我正在尝试制作一个登录系统,但阻止我的是我无法调试的名称错误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
查看完整描述

2 回答

?
慕标5832272

TA贡献1966条经验 获得超4个赞

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("Enter Age:"))


theinfo = [user_signup, user_signup2, user_signup3]


print()

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

print()


if know == "l":

    login = input("Enter username: ")

    user_login2 = input("Enter password:")


    if login in theinfo and user_login2 in theinfo:

        print("Log in successful")

    elif login not in theinfo or user_login2 not in theinfo:

        print("username or password incorrect")

NameError意味着该变量之前没有定义。您为程序设置条件的方式是错误的。我已经修正了程序。


查看完整回答
反对 回复 2023-10-25
?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

使用 if / elif,将读取零个或一个代码段。让我们看看你的 if / elif / elif 代码:


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

如果用户输入“l”,则设置登录。但是,接下来的两个“elif”部分将被跳过。你可以这样解决这个问题:


if know =="l":

    login = input("Enter username: ")

    user_login2 = input("Enter password:")

    if 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")

那么,这就回答了问题。然而,由于多种原因,该代码仍然无法按预期工作。但是,您主要缺少一个用于存储完整用户集的凭据列表。您没有现有用户的凭据,因此无法检查返回用户的用户名和密码。您也不能将新用户的凭据保存到现有用户列表中。


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.  

# Don't do this in your actual code.  You can use something

# like a simple file (though you should not store clear text

# text passwords in a text file. 

# https://www.w3schools.com/python/python_file_handling.asp

credentials = [['Phil', 'my_Password', 37], ['jose', 'espanol', 19]]


the_info = []


if know == 'S':

    user_signup = input('Enter username:')

    user_signup2 = input('Enter password: ')

    user_signup3 = int(input('Enter age:'))

    the_info = [user_signup , user_signup2, user_signup3]

    # The next line save the new user in memory.  

    # Add code to save the info to a file or database. 

    credentials += the_info

    print('Thanks for signing up.')

elif know =='L':

    login = input('Enter username: ')

    user_login2 = input('Enter password: ')

    success = False

    for credential in credentials:

        if login.upper() == credential[0].upper() and user_login2 == credential[1]:

            the_info = credential

            break

    if len(the_info) > 0 :

        print(f'Log in successful.  Hello {the_info[0]}')

    else:

        print('username or password incorrect.')


print(f'User info: {the_info}')


查看完整回答
反对 回复 2023-10-25
  • 2 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信