Slot Machine Game in Python- Repetition of Lines

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, it keeps on repeating 2 statements, here's the output I get when I input "100":

Slot Machine You have 1000 coins. Press 0 to exit, any other number to play that coins per spin. 100 8 5 3 You lost 100 . You now have 900 coins. Press 0 to exit, any other number to play that coins per spin. You have 900 coins. Press 0 to exit, any other number to play that coins per spin.

-it repeats "Press 0 to exit, any other number to play that coins per spin." and "You have () coins."

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

There are 2 answers

1
iChux On

I think this is a great opportunity for you to begin to try out debuggers in your IDE of choice. Try to step through the code to see the execution. Stepping through the code will also afford you the opportunity to see many nuances and learn along the way.

Most importantly, don't forget to document what your problems are, the solution(s) and maybe how you arrived at it. You just might not say when you'll need to refer to it again.

0
jgritty On

There are a number of little issues that I see here.

First, simply move these 2 lines above the while statement:

print "You have",coins, "coins."
print "Press 0 to exit, any other number to play that coins per spin."

That will stop the duplication of text.

Next, on the loss, you have a comma at the end of the very last print line. Just remove that, so there is a newline printed.

Another issue I see that may be intended is that if y == z, you don't win, but I think you intend for that to be a win.

So change this line:

elif x==y or x == z:

To this:

elif x==y or x == z or y==z:

The last thing I see is that the game doesn't end when you press 0.

Just add a line like this:

if wager == 0:
    break

That's all I see. Here's your whole program:

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 coins > 0:

    wager = input("")
    if coins == 0:
        break
    if wager == 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."