Python: Slot Machine Code Error

1.8k views Asked by At

I'm a beginner at using python in wing ide and I'm trying to write a slot machine program. It is not running properly, and I know it has something to do with the infinite loop but I'm not sure how to fix that loop so the code runs properly. Any ideas?

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("")
1

There are 1 answers

3
riri On BEST ANSWER

Ok, so you may have misunderstood looping. This code is probably what you want:

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

Notice that now everything is inside one loop that checks if your coins is greater than zero? Before the loop with the random number generation could never exit and move on to the rest of the program.