Lucky Sevens游戏(初学者)

问题描述:

我很难让程序重复循环,直到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

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

有几件事错。首先,由于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的值不会被重新分配。