user2027690
user2027690

Reputation: 459

Loop breaking every time in slot machine program

I'm trying to break the loop by saying

if deff <= int(total):
    break

but the loop will break regardless of the input being negative or more than the total it will break the loop

any advice of what am i doing wrong?

P.S i know i will new a formula to decide if player won or lost. right now im just trying to figure out the first code before going there

first time on programming and teacher not helpful ):


def intro():

    greeting()

    print('How much money do you want to start with?')

    print('Enter the starting amount of dollars.', end='')
    total = int(input())

    print('Your current total is '+ str(total)+'\n')
    while True:

        print('How much money do you want to bet (enter 0 to quit)?', end='');
    # Bett will be the amount of money that the player will play with

        bett = int(input())
        if bett > int(total):
            print('ERROR You don\'t have that much left')

        if bett < int(0):
            print('ERROR: Invalid bet amount\n')

        if bett <= int(total)
            break

# Function shows results of slot machine after handle being pulled
def random():
    import random

    num1 = random.randint(1, 5)
    num2 = random.randint(1, 5)
    num3 = random.randint(1, 5)

    print('/---+---+---\  ')
    print('|-'+ str (num1)+'-|-'+ str(num2) +'-|-'+ str (num3) +'-|')
    print('\---+---+---/  ')



intro()

Upvotes: 1

Views: 475

Answers (3)

Ber
Ber

Reputation: 41873

You need to use elif and else in the successive conditional tests:

    bett = int(input())
    if bett > total:
        print('ERROR You don\'t have that much left')

    elif bett < 0:
        print('ERROR: Invalid bet amount\n')

    else:
        break

That way, only one of the statements in executed, instead of more or more.

NB: It's not necessary to use theint() constructor all the time on something that is already an int

Upvotes: 2

Aslan
Aslan

Reputation: 1

If it were me, I would use a while loop.

play = True
while play == True:
    #all the code
    #to be executed
    #indented here
    #
    #Ask user if they want to continue playing
    p = input("Play again?[y/n] ")
    playAgain = str(p)

    if playAgain == "y":
        play = True
    else:
        play = False

Upvotes: 0

askewchan
askewchan

Reputation: 46558

In this block:

    if bett <= int(total)
        break

You have a syntax error. Add a colon to the end of hte first line:

    if bett <= int(total):
        break

Upvotes: 0

Related Questions