亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何調試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 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號