[英]While loop looping forever Python
因此,我正在研究craps程序模擬器,既可以在網上瀏覽,也可以從該論壇的用戶那里獲得一些幫助。 我大部分時間都能完成。 至少,我認為我具有游戲邏輯。
我上次使用它時正在運行它,但是我必須更改了某些東西而沒有意識到它或某些東西,因為現在while循環一直在運行。 在該程序中,骰子永遠滾動而無需用戶干預。
有人可以看一下代碼是否有問題嗎? 我已經為此工作了幾個小時,卻一無所獲。
我再也不會賭博了! :/
from Dice import PairOfDice
print("Now Let's play with two dice!")
#
def MainDouble():
bdice = PairOfDice()
doubleDiceRoll = ''
global myPoint #declare global variable
input("Press Enter to Roll the Dice once")
while doubleDiceRoll == '': #Roll the dice (both die objects)
bdice.toss()
print ('The first die reads.. ' + str(bdice.getValue1()) + '\n')
print ('The second die reads.. ' + str(bdice.getValue2()) + '\n')
Val = bdice.getTotal()
##Beginning of conditional blocks##
if Val == 7 or Val == 11:
gamestatus = "WON"
elif Val == 2 or Val == 3 or Val == 12:
gamestatus = "LOST"
if Val != 7 and bdice.getTotal() != 11 and Val != 2 and Val != 3 and Val != 12:
gamestatus = "CONTINUE"
#
myPoint = Val
print("The Point is now " + myPoint + "/n") #display the user's point value
global pSum
pSum = 0
#The point
while gamestatus == "CONTINUE": #Checking the point
global point
point = myPoint
pSum = MainDouble()
if pSum == myPoint:
gamestatus == "WON"
elif pSum == 7:
gamestatus = "LOST"
if gamestatus == "WON":
print("Winner!")
else:
print("Sorry, Seven out!")
print ("Roll Again?")
doubleDiceRoll = input("Roll Again?")
MainDouble()
該塊:
while doubleDiceRoll == '': #Roll the dice (both die objects)
bdice.toss()
print ('The first die reads.. ' + str(bdice.getValue1()) + '\n')
print ('The second die reads.. ' + str(bdice.getValue2()) + '\n')
Val = bdice.getTotal()
doubleDiceRoll
永遠不會從''
更改,因此此循環將永遠運行。 在該塊的末尾(但仍在其中!),您應該執行類似的操作
doubleDiceRoll = raw_input("Roll Again?") #Edit thanks to @Adam Smith
我認為您在以下情況后弄亂了縮進:
##Beginning of conditional blocks##
該行下方的所有內容都在while循環之外,但可能應該在其中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.