代碼中有幾處錯誤,無論是語法還是邏輯。在你進一步學習之前,我建議你回到閱讀基本的Python教程,直到你至少能夠很好地理解語言的語法。
同時,下面是代碼的功能版本,經過嚴格評論,以便您可以通過它瞭解發生了什麼。
# first we declare our valid username & password
username = "chicken"
password = "monkeys"
# we print our welcome message
print("Welcome to example")
# Then we ask our initial question
input_question = input("Do you have an example account (Please type yes or no)?")
# We only care if input was yes, otherwise
# program will terminate
if input_question == "yes":
# then we initiate an infinite loop
while True:
# in each iteration, we prompt user to input
# a username and password
input_username = input("please input username")
input_password = input("Please input your password")
# and for each iteration, we check the input
if input_username == username:
if input_password == password:
# if we reach this point, user has entered good
# credentials, we print our success message and
# break the loop
print("access granted")
break
# if we reach this point, credentials didn't match
# we print error message and let our loop reinitiate
print("access denied")
有這個代碼這麼多錯,我甚至不知道從哪裏開始。 – Verv
**(1)**布爾表達式的評估方式與您的大腦評估英語語法的方式不同。 '用戶名和密碼== True'的字面意思是用戶名和(密碼== True)'不是'(用戶名== True)和(密碼== True)'。 **(2)**您需要使用'='而不是'=='分配變量值。 **(3)** [縮進在Python中很重要](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it)。 –
您的腳本中有幾處錯誤。你能告訴我,你在哪裏檢查,如果用戶名 - 密碼組合是有效的? –