1

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

3
  • The code works for me (after obvious corrections), what problem do you have exactly?
    – wRAR
    Feb 21, 2013 at 23:38
  • 1
    I would advise against naming your function random since you already have a module named random. Technically it works but you should be careful.
    – askewchan
    Feb 21, 2013 at 23:44
  • Are you sure it breaks when bett is greater than total, as well as when it's negative? Feb 21, 2013 at 23:52

3 Answers 3

2

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

7
  • 1
    This is not strictly necessary and doesn't solve any problems unless I'm missing something.
    – wRAR
    Feb 21, 2013 at 23:42
  • 2
    No, this should do it. The first elif isn't strictly necessary, since the first and second conditions can't both be true (assuming total > 0, but it's still clearer to include it. Feb 21, 2013 at 23:44
  • @wRAR OP's problem. (Though it is missing a colon.) Feb 21, 2013 at 23:46
  • @iamnotmaynard I don't have problems with OP's code (not counting the missing colon).
    – wRAR
    Feb 21, 2013 at 23:47
  • @wRAR OP does, though. The while loop was breaking when it shouldn't have (or rather, when he didn't intend it to). Feb 21, 2013 at 23:51
0

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
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.