carmella diaz
carmella diaz

Reputation: 39

Python keeps looping after termination message?

I am having some trouble now with getting some user input to stop looping after the termination message, Basically, once 0 (zero) is entered to end the program, a message will be printed saying "Thanks for playing." That there should be the end of it. However, it continues to loop by asking for the next line of input "Choose a number between 2 and 12." It is a little difficult to explain... I know a break or exit() will fix my problem, but those are not acceptable solutions. Basically, once 0 (zero) is entered on the bet input, I need it to finalize and print a termination message not continue on with the program.

I need this output:

you have $500 in your bank # starting amount
Enter bet (or 0 to quit): 0 
Thanks for playing!

instead, I get this:

Enter bet (or 0 to quit): 0
Thanks for playing!
Choose a number between 2 and 12: # where the program continues to run 
# when it shouldn't. The user should only see this input field if they enter
# number above 0

This is the code

import random

def rollDice(cnt):
    die1 = random.randint(1,6)
    die2 = random.randint(1,6)
    x = int(die1 + die2)
    print('Roll #', cnt, 'was', x)
    return x

def total_bank(bank):
    bet = 0
    while bet <= 0 or bet > min([500,bank]):
        print(f'You have ${bank} in your bank.')
        get_bet = input('Enter your bet (or 0 to quit): ')
        bet = int(get_bet)
        if get_bet == '0': 
            print('Thanks for playing!')
            return bank, bet
        return bank, bet

def get_guess():
    guess = 0
    while (guess < 2 or guess > 12):
        try:
            guess = int(input('Choose a number between 2 and 12: '))
        except ValueError:
            guess = 0
    return guess

prog_info()
bank = 500
guess = get_guess
rcnt = 1

while rcnt < 4:
    rcnt = 0
    bank,bet = total_bank(bank)
    guess = get_guess()
    if guess == rollDice(rcnt+1):
        bank += bet * 2
    elif guess == rollDice(rcnt+2):
        bank += bet * 1.5
    elif guess == rollDice(rcnt+3):
        bank = bank
else:
    bank = bank - bet
    if bank == 0:
        print(f'You have ${bank} in your bank.')
        print('Thanks for playing!')

Upvotes: 0

Views: 40

Answers (1)

Prune
Prune

Reputation: 77837

The problem is in your comparison:

        guess = int(input('Choose a number between 2 and 12: '))
        if guess == '0':

guess is an integer: you explicitly converted it. It can never be equal to a string. Instead, compare to the integer value:

        guess = int(input('Choose a number between 2 and 12: '))
        if guess == 0:

Upvotes: 1

Related Questions