-1

I'm a very beginner in computer language (2 days) and am writing a basic slot machine code in python.

import random
import time

balance = 1000

print("Welcome to the Slot World")

player_name = input("What's your name?")


while balance > 0:

    print(player_name, ", you have $",balance, " to bet.")
    bet = int(input("How much do you want to bet?"))

    x = random.randint(1, 5)
    y = random.randint(1, 5)
    z = random.randint(1, 5)
    numbers = [x, y, z]

    if bet != int:
        print("Please enter correct amount")
        continue
    elif bet > balance:
        print("You do not have sufficient amount")
        continue
    elif bet <= 0:
        print("Please enter correct amount")
        continue
    else:
        print("You have bet $", bet)
        time.sleep(0.5)
        print(numbers)
        balance = balance - bet

    if x == y and y == z:
        if x == 1:
            print("Winner 20x!")
            bet = bet * 20
            balance = balance + bet
        elif x == 2:
            print("Winner 30x!")
            bet = bet * 30
            balance = balance + bet
        elif x == 3:
            print("Winner 40x!")
            bet = bet * 40
            balance = balance + bet
        elif x == 4:
            print("Winner 50x!")
            bet = bet * 50
            balance = balance + bet
        elif x == 5:
            print("Winner 100x!")
            bet = bet * 100
            balance = balance + bet

    elif x == y and not y == z:
        if x == 1:
            print("Winner 2x")
            bet = bet * 2
            balance = balance + bet
        elif x == 2:
            print("Winner 3x")
            bet = bet * 3
            balance = balance + bet
        elif x == 3:
            print("Winner 4x")
            bet = bet * 4
            balance = balance + bet
        elif x == 4:
            print("Winner 5x")
            bet = bet * 5
            balance = balance + bet
        elif x == 5:
            print("Winner 10x")
            bet = bet * 10
            balance = balance + bet

    elif not x == y and y == z:
        if y == 1:
            print("Winner 2x")
            bet = bet * 2
            balance = balance + bet
        elif y == 2:
            print("Winner 3x")
            bet = bet * 3
            balance = balance + bet
        elif y == 3:
            print("Winner 4x")
            bet = bet * 4
            balance = balance + bet
        elif y == 4:
            print("Winner 5x")
            bet = bet * 5
            balance = balance + bet
        elif y == 5:
            print("Winner 10x")
            bet = bet * 10
            balance = balance + bet

    elif not x == y and x == z:
         if x == 1:
                print("Winner 2x")
                bet = bet * 2
                balance = balance + bet
         elif x == 2:
                print("Winner 3x")
                bet = bet * 3
                balance = balance + bet
         elif x == 3:
                print("Winner 4x")
                bet = bet * 4
                balance = balance + bet
         elif x == 4:
                print("Winner 5x")
                bet = bet * 5
                balance = balance + bet
         elif x == 5:
                print("Winner 10x")
                bet = bet * 10
                balance = balance + bet

    else:
        print("Bad Luck! You Lost!")
print("You lost all your money\nThe End")

If I input a character for bet

bet = int(input("How much do you want to bet?"))

it gives me

ValueError: invalid literal for int() with base 10: 'a'
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50

4 Answers4

1

You can use try:

while True:
    try:
        bet = int(input("How much do you want to bet?"))
    except ValueError:
        print "Not valid integer"
        continue
    break
Joe
  • 12,057
  • 5
  • 39
  • 55
1

You can not change string like a-Z into integer so you have to check for it

try:
    bet = int(input("How much do you want to bet?"))
except ValueError:
    print ("Not valid integer")
    continue
utks009
  • 573
  • 4
  • 14
0

Firstly, the letter a can't be converted to a number, because what to which number should it then convert?

So for that @Joe's answer with try try except should be perfect.

Also, it might be safer to rather convert to a float. It makes it more robust, you can enter decimal values if you want,

bet = float(input("How much do you want to bet?"))
Hein Wessels
  • 937
  • 5
  • 15
0

Use int() function to try to convert string value to integer. It will raise ValueError if it is not. Use try to catch it and use its else clause to break from the loop.

while True:
    try:
        value = int(input('Enter your number'))
    except ValueError:
        pass
    else:
        break
leotrubach
  • 1,509
  • 12
  • 15