2012-02-20 99 views
0

我很難讓程序重複循環,直到mypot != 0。該計劃的目的是詢問用戶的金額放於鍋中,並要求用戶擲骰子直到mypot !=0Lucky Sevens遊戲(初學者)

import random 

mypot = int(input("Please enter the amount of money you want in the pot: ")) 
diceroll1 = random.randint(1, 7) 
diceroll2 = random.randint(1, 7) 
myrole = (diceroll1 + diceroll2) 
while True: 
    mypot > 0 
if myrole == 7: 
    print("Your roll was a 7 you eaerned press enter to roll again: ", mypot + 4) 
    break 
elif myrole != 0 
    print("Sorry you did not roll a 7 press enter to roll again: ", mypot - 1) 
    break 
elif mypot != 0: 
    print("Sorry you do not have no more money in your pot") 
    break 

更新:我有麻煩的程序重複循環直到mypot == 0

import random 

mypot = int(input("Please enter the amount of money you want in the pot: ")) 
while mypot > 0: # looping untill mypot <= 0 
    dice_roll = random.randint(1, 7), random.randint(1, 7) # rolling the dices 
print(dice_roll[0], dice_roll[1]) 
myrole = sum(dice_roll) 
if myrole == 7: 
    mypot += 4 # without this you're not modifing the myrole value 
    print("Your roll was a 7 you earned. Press enter to roll again:", mypot) 
else: 
    mypot -= 1 
    print("Sorry you did not roll a 7. Press enter to roll again:", mypot) 
input() # waiting for the user to press Enter 

print("Sorry, there's no more money in your pot.") 
+0

用四個空格修復縮進。 – eumiro 2012-02-20 13:25:08

回答

1

while循環是很奇怪的:elif mypot != 0永遠不會被cheked,怎麼一回事,因爲無論是之前的條件之一將是真實的,所以你的while循環是不是也是一個LO一點都不。

爲了解決這個問題,你需要改變:

​​

有:

if mypot != 0: 
    print "Sorry you do not have no more money in your pot" 
    break 

而其他IFS之前把這個。

但你的代碼似乎有其他問題:

有:

mypot > 0 

while True後沒事,爲什麼它不是:

while mypot > 0: 
    # ... 

同樣的myrole值不似乎在while循環中修改,那麼while非循環在那裏做什麼?

DSM表明您正在嘗試使用非循環作爲開關的while

我認爲你試圖做一些事情,如:

import random 

mypot = input("Please enter the amount of money you want in the pot: ") 
# or as alternative: 
#mypot = int(raw_input("Please enter the amount of money you want in the pot: ")) 
while mypot > 0: # looping untill mypot <= 0 
    dice_roll = random.randint(1, 7), random.randint(1, 7) # rolling the dices 
    print dice_roll[0], dice_roll[1] 
    myrole = sum(dice_roll) 
    if myrole == 7: 
     mypot += 4 # without this you're not modifing the myrole value 
     print "Your roll was a 7 you earned. Press enter to roll again:", mypot 
    else: 
     mypot -= 1 
     print "Sorry you did not roll a 7. Press enter to roll again:", mypot 
    raw_input() # waiting for the user to press Enter 

print "Sorry, there's no more money in your pot." 
+1

實際上,修復語法錯誤之後,while循環將始終*中斷。如果在第一個分支中的分數是7,或者它會在第二個分支中分解,它將會中斷。我認爲OP在這裏使用「break」,就像在C中的switch語句中使用「break」一樣,即脫離鏈接的ifs。 – DSM 2012-02-20 13:31:53

+0

@DSM:你說得對,我給了一個「太快」的代碼,謝謝。我會更新我的答案。 – 2012-02-20 13:35:34

+0

同意Rik - 我看不到'mypot'變量正在被修改。 – halfer 2012-02-20 13:36:24

0

有幾件事錯。首先,由於python使用了所謂的語義空白,所以縮進很重要。所以你有它

while True: 
mypot > 0 
if myrole == 7: 
    print("Your roll was a 7 you eaerned press enter to roll again: ", mypot + 4) 
    break 

它讀這個爲:而真,評估(mypot> 0)。然後轉到if語句。你的elifs也應該和你的if語句的縮進級別相同,並且在你的第一個elif之後你需要冒號。

無論如何,這可能不是最大的問題。在你的循環內,你沒有任何改變。你需要把它移到True,直到diceroll1上面的ilne。 True後面的所有代碼:將反覆執行,並且在while循環中,myrole的值不會被重新分配。