Python 幸运七人游戏(初学者)
我无法让程序重复循环,直到Python 幸运七人游戏(初学者),python,Python,我无法让程序重复循环,直到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)
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.")
随机导入
mypot=int(输入(“请输入您想要在pot中输入的金额:”)
while mypot>0:#循环直到mypot您的while循环非常奇怪:elif mypot!=0
将永远不会被选中,因为前面的任何一个条件都将为真,因此您的while循环也不是一个循环
要修复您需要更改的问题,请执行以下操作:
elif mypot != 0:
print "Sorry you do not have no more money in your pot"
break
与:
把这个放在其他假设之前
但您的代码似乎还有其他问题:
有:
mypot > 0
就在之后,而True
,为什么它不是:
while mypot > 0:
# ...
而且myrole
的值在while
循环中似乎没有被修改,那么while
非循环在那里做什么呢
DSM建议您尝试使用而非循环作为开关
我想你是在尝试这样做:
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."
随机导入
mypot=input(“请输入您想要在pot中存入的金额:”)
#或作为替代方案:
#mypot=int(原始输入(“请输入您想要的金额:”)
当mypot>0时:#循环直到mypot这有一些问题。首先,由于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语句。您的elif应该与if语句处于相同的缩进级别,并且在第一个elif之后需要一个冒号
无论如何,这可能不是最大的问题。在你的循环中,你没有任何变化。您需要将while-True:statement向上移动到diceroll1正上方的ilne。while True之后的所有代码:将反复执行,在while循环中,myrole的值不会重新分配。用四个空格修复缩进。实际上,在修复语法错误后,while循环将始终中断。如果卷在第一个分支中为7,它将断裂,或者在第二个分支上断裂。我认为OP在这里使用的是“break”,就像人们在C语言的switch语句中可能使用的那样,也就是说,为了摆脱链式的ifs。@DSM:你说得对,我对代码看得“太快了”,谢谢。我会更新我的答案。同意Rik-我也看不到mypot
变量在哪里被修改。@Andreharisii:您正在打印新的mypot
值,但是如果您不将该值重新分配回mypot
它的值将不会改变。我试图修改您的示例,看一看。@RikPoggi:我认为他正在运行Python 2,所以他的input()需要一些东西作为输入。(这与他的印刷风格不符,但可以解释错误。)
while True:
mypot > 0
if myrole == 7:
print("Your roll was a 7 you eaerned press enter to roll again: ", mypot + 4)
break