這是一個螃蟹模擬器。我的while循環有問題。它在哪裏說程序停留在while循環但不運行程序?
while val == True:
是問題發生的地方。它停留在while循環中,但沒有任何反應。如果您發現任何問題,我將非常感激。 這裏是完整的代碼。 (我曾嘗試來驗證一切)通過程序x
和val
都是False
import time
import random
control1 = False
control2 = True
x = True
val = True
z = True
def throw(n):
for i in range(1,n+1):
dice_1 = random.randint(1,6);dice_2 = random.randint(1,6)
print "roll",i,", die 1 rolled",dice_1,"and die 2 rolled",dice_2,",total",dice_1+dice_2
time.sleep(2)
return n
while z == True:
if x == True:
while control1 == False:
try:
amount_1 = int(raw_input("Welcome to crabs.\nHow many times would you like to roll:"))
control1 = True
except ValueError:
print ("Enter a valid number.")
throw(amount_1)
x = False
else:
while val == True:
roll_again = raw_input("Would you like to roll again: ")
if roll_again == "1":
val = False
while control2 == True:
try:
amount_2 = int(raw_input("How many times would you like to roll:"))
control2 = False
except ValueError:
print ("Enter a valid number.")
throw(amount_2)
z = True
elif roll_again == "2":
val = False
exit()
else:
val = True
'while z == True' may be''while z' because it's same。不需要'重複檢查'布爾值;)你也應該給你的變量顯式名稱...... – Depado
爲了幫助調試,爲什麼不在每個'if'分支中做一個簡單的'print'? –
Your final else:val = True並不是必需的,因爲val在while val內已經是true:當你說它什麼都不做時,你的意思是說你沒有看到raw_input命令的提示嗎? – sabbahillel