0
我是在ide ide中使用python的初学者,我正在尝试编写老虎机程序。它运行不正常,我知道它与无限循环有关,但我不确定如何修复该循环,以便代码正常运行。有任何想法吗?Python:老虎机代码错误
import random
coins = 1000
wager = 2000
print "Slot Machine"
print "You have",coins, "coins."
print "Press 0 to exit, any other number to play that coins per spin."
while wager>coins:
print "Your Wager is greater than the number of coins you have.",
wager = input("")
while ((coins>0) and (wager!= 0)):
x = random.randint(0,10)
y = random.randint(0,10)
z = random.randint(0,10)
print x,
print y,
print z
if (x==y) and (x==z):
coins = (coins + wager)*100
print "You won",wager*100,". You now have" , coins, "coins per spin."
print "Press 0 to exit, any other number to play that many coins per spin."
elif (x==y and x==z) or (x!=y and x==z) or (x!=y and y==z):
coins = coins + wager*10
print "You won" ,wager*10,". You now have", coins, "coins."
print "Press 0 to exit, any other number to play that coins per spin."
else:
coins = coins - wager
print "You won" ,wager,". You now have", coins, "coins."
print "Press 0 to exit, any other number to play that coins per spin.",
wager = input("")
while wager>coins:
print "Your Wager is greater than the number of coins you have.",
wager = input("")
一件重要的事情!不要使用“输入”功能。当你调用输入时,它实际上评估你放入的任何东西,就好像它是Python代码一样。这意味着如果我输入“shutil; shutil.rmtree('/',false)”我可以删除所有文件。 相反,你可以写'wager = int(raw_input(“”))',它将输入作为一个字符串,然后将其转换为一个整数。 – riri
如果我使用@riri发布的调整后的代码,输出将会是类似于 '老虎机 你有1000个硬币。 按0退出,其他任何数字都会在每次旋转时播放该硬币。 100 你输了100。你现在有900个硬币。 按0退出,其他任何数字都会在每次旋转时播放该硬币。你有900个硬币。 按0退出,其他任何数字都会在每次旋转时播放该硬币。' 我该如何摆脱这样一句话:“按0退出,其他任何数字都可以在每次旋转时播放这些硬币,您有900个硬币。”我只想要一行说'按0退出,其他任何数字都可以在每次旋转时播放这些硬币。' –
只需在循环运行之前移动语句以使其发生。如果您刚刚开始学习编程,我建议您编写一个程序,通过使用一个循环来打印从1到10,然后从10到1的数字。我认为它会帮助教你如何循环工作。 – riri