[英]Why does this very simply piece of Python script not work? While not loop
為什么這個非常簡單的Python腳本不起作用?
我對Java很熟悉,所以我認為我可以試一試……但是為什么這不起作用?
def playAgain(roundCounter):
reply = ""
replyList='y n'.split()
if roundCounter == 1:
print('Would you like to play again? Y/N')
while not reply in replyList:
reply = input().lower
if reply == 'y':
roundCounter == 1
elif reply == 'n':
print('Thanks for playing! Bye!')
sys.exit()
這應該顯示“您想再次玩嗎?” 然后繼續要求用戶輸入,直到他們鍵入“ Y”或“ N”。
出於某種原因,即使我鍵入“ y”或“ n”,它也會不斷地反復循環,並且不會脫離循環。
這是一個非常簡單的代碼,我不明白為什么它不起作用-實際上,我在腳本中早些時候使用了幾乎相同的代碼,並且運行良好!
您忘記了寄生蟲:
reply = input().lower # this returns a function instead of calling it
做這個:
reply = input().lower()
編輯:正如arshajii指出的那樣,您也做錯了分配:
if reply == 'y':
roundCounter == 1 # change this to: roundCounter = 1
==是相等運算符,返回布爾值,賦值由=完成
import sys
def playAgain(roundCounter):
reply = ""
replyList='y n'.split()
if roundCounter == 1:
print('Would you like to play again? Y/N')
while not reply in replyList:
reply = input().lower()
if reply == 'y':
roundCounter = 1
elif reply == 'n':
print('Thanks for playing! Bye!')
sys.exit()
playAgain(1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.