我已經搜索了網路和這個網站,整天都在胡鬧,嘗試了 100 種方法來讓這個簡單的小程式正常作業。我正在練習無休止的 While 回圈和字串用戶輸入。誰能解釋我做錯了什么?謝謝!
while True:
print("This is the start.")
answer = input("Would you like to continue? (Y/N) ")
answer = answer.islower()
if answer == "n":
print("Ok thank you and goodbye.")
break
elif answer == "y":
print("Ok, let's start again.")
else:
print("You need to input a 'y' or an 'n'.")
uj5u.com熱心網友回復:
您的代碼有一個錯誤answer.islower()將回傳布林值 True 或 False 但您想將其轉換為較低的值,因此正確的方法將是answer.lower()
while True:
print("This is the start.")
answer = input("Would you like to continue? (Y/N) ")
answer = answer.lower() # change from islower() to lower()
if answer == "n":
print("Ok thank you and goodbye.")
break
elif answer == "y":
print("Ok, let's start again.")
else:
print("You need to input a 'y' or an 'n'.")
uj5u.com熱心網友回復:
您只需要對這一行進行一項修改:
代替
答案 = answer.islower()
改成
答案 = answer.lower()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/526542.html
上一篇:使用Python處理文本檔案
