2 回答

TA貢獻1966條經(jīng)驗 獲得超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意味著該變量之前沒有定義。您為程序設(shè)置條件的方式是錯誤的。我已經(jīng)修正了程序。

TA貢獻1827條經(jīng)驗 獲得超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(fā)”,則設(shè)置登錄。但是,接下來的兩個“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")
那么,這就回答了問題。然而,由于多種原因,該代碼仍然無法按預(yù)期工作。但是,您主要缺少一個用于存儲完整用戶集的憑據(jù)列表。您沒有現(xiàn)有用戶的憑據(jù),因此無法檢查返回用戶的用戶名和密碼。您也不能將新用戶的憑據(jù)保存到現(xiàn)有用戶列表中。
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}')
添加回答
舉報