我是在wing ide中使用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("")
答案 0 :(得分:0)
好的,所以你可能误解了循环。这段代码可能就是你想要的:
import random
coins = 1000
wager = 2000
print "Slot Machine"
while coins > 0:
print "You have",coins, "coins."
print "Press 0 to exit, any other number to play that coins per spin."
wager = input("")
if coins == 0:
break
while wager>coins:
print "Your Wager is greater than the number of coins you have.",
wager = input("")
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 or x == 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 lost" ,wager,". You now have", coins, "coins."
print "Press 0 to exit, any other number to play that coins per spin.",
请注意,现在一切都在一个循环中,检查你的硬币是否大于零?在使用随机数生成的循环之前,永远不会退出并继续执行程序的其余部分。