我想在Python上做一個骰子21遊戲(如果你需要查找,在這裏輸入的時間太長)。它還沒有完成,但現在我正在經歷並修復我犯的任何錯誤。我有一些while循環不會關閉的問題。在玩家選擇堅持diceroll函數之後,它應該將playeraddrolls設置爲False並退出while循環,進入到computeroll函數中。但是,它只是循環回來。需要立即的幫助,因爲這是週一的一個學校項目,在我仍然需要完成代碼之後。如果您能指出我稍後會遇到的任何其他錯誤,以及如何解決這些錯誤,這也會有所幫助。Python while循環不停止?
import random
stick=0
winner=[""]
def diceroll(addedrolls,stick,playagain,playagain1or2,playeraddedrolls,computeraddedrolls,playeraddrolls):
while playeraddedrolls<21 or playeraddrolls is True:
stick=0
die1=random.randint(1,6)
die2=random.randint(1,6)
print("You rolled ",die1,"and ",die2,".")
playeraddedrolls=die1+die2
if playeraddedrolls>21:
print("You rolled over 21. Computer wins by default!")
computeraddedrolls(playeraddedrolls,playagain,playagain1or2,computeraddedrolls)
else:
while stick>2 or stick<1:
stick=int(input("Press 1 to stick or 2 to roll again. "))
if stick==1:
print("You chose to stick at", playeraddedrolls,". The computer will now roll.")
playeraddrolls=False
computeroll(playeraddedrolls,playagain,playagain1or2,computeraddedrolls)
elif stick==2:
print("You chose to roll again. Producing numbers now.")
else:
print("I'm sorry, that's not a valid command.")
def computeroll(playeraddedrolls,playagain,playagain1or2,computeraddedrolls):
while computeroll<17:
die3=random.randint(1,6)
die4=random.randint(1,6)
print("The comoputer rolled ",die3,"and ",die4,".")
computeraddedrolls=die3+die4
if playeraddedrolls>21:
winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
else:
if computeraddedrolls<17:
print("The computer chose to roll again!")
elif computeraddedrolls>21:
print("The computer rolled over 21, you win by default!")
winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
else:
print("Overall, the computer scored ", computeraddedrolls,".")
winningtally(playeraddedrolls,computeraddedrolls,playagain,playagain1or2)
def winningtally(PAR,CAR,playagain,playagain1or2):
if playeraddedrolls>21 or computeraddedrolls>playeraddedrolls:
print("I have added the computers win to the tally. Here is the new set of wins:")
append(computer)
print(winner)
playagain(PAR,CAR,playagain,playagain1or2)
elif computeraddedrolls>21 or playeraddedrolls>computeraddedrolls:
print("I have added your win to the tally. Here is the new set of wins:")
append(player)
print(winner)
playagain(PAR,CAR,playagain,playagain1or2)
def playagain(PAR,CAR,playagain,playagain1or2):
while playagain1or2<1 or playagain1or2>2:
playagain1or2=int(input("Press 1 to play again, or 2 to view the final result."))
if playagain1or2==1:
print("Okay, rerunning...")
return
elif playagain1or2==2:
computerwins=(winner).count(computer)
playerwins=(winner).count(player)
if computerwins>playerwins:
print("Sorry, the computer won. Better luck next time!")
else:
print("Congratulations, you won! Thank you for playing!")
else:
print("I'm sorry, ",playagain1or2," is not a valid command.")
playeraddrolls=True
playeraddedrolls=2
computeraddedrolls=2
playagain1or2=0
playagain=True
while playagain==True:
stick=0
addedrolls=3
diceroll(addedrolls,stick,playagain,playagain1or2,playeraddedrolls,computeraddedrolls,playeraddrolls)
打開Python解釋器並鍵入False <21'。結果將解釋您的問題。其解釋是,布爾運算符「False」和「True」只是整數的特殊情況 - 0和1. –
將變量重新定義爲* integer *或* True可能不是一個好主意/假。使用一個單獨的變量。 –