我无法让程序重复循环直到mypot != 0
。该程序的目的是询问用户放入底池的金额,并要求用户掷骰子直到mypot !=0
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 :(得分:1)
你的while循环非常奇怪:elif mypot != 0
永远不会被打翻,因为之前的一个条件都是True,所以你的while循环也不是一个循环。
要解决此问题,您需要更改:
elif mypot != 0:
print "Sorry you do not have no more money in your pot"
break
使用:
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 :(得分: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&gt; 0)。然后转到if语句。你的elifs也应该与if语句的缩进程度相同,并且你的frist elif之后你需要一个冒号。
无论如何,这可能不是最大的问题。在你的循环中,你没有任何改变。你需要将它移动到True:声明直到在diceroll1之上的ilne。在True之后的所有代码:将会反复执行,而在while循环中,myrole的值现在不会被重新分配。