0

我是在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("")
4

1 回答 1

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.",

请注意,现在所有内容都在一个循环中,用于检查您的硬币是否大于零?在生成随机数的循环永远无法退出并继续执行程序的其余部分之前。

于 2014-01-06T23:33:45.437 回答