carmella diaz
carmella diaz

Reputation: 39

Is it possible to end a while loop with a try/except statement?

So the goal here is to end my while loop, or my script really if a user's bank amount reaches 0 (zero) or if the user inputs a 0 (zero) for bet amount to terminate the script. THE PROBLEM is that I am NOT allowed to use a break statement, and though the program works perfectly with exit(), I cannot use that either because we haven't "gotten there yet" via the book (the instructor wants us to use what we are learning in the book and not what we already know). I am just really stumped. Part of it is because I struggle a lot with the try/except statements so maybe I am just doing something wrong there, or maybe I need to try a different loop. Maybe you guys can help? Gonna post my code below. As far as I am concerned, everything works perfectly fine, but I cannot use exit() or break so I need some advice on another python basics-orientated method... So can I use try/except? If so, how? I need the program to continue looping unless banking reaches 0 (zero) or user input is equal to 0 (zero), it's why I chose the while loop. Do I need to use a different loop method? The help will be GREATLY appreciated and I thank everyone in advance.

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): ')
        if get_bet == '0': 
            print('Thanks for playing!')
            exit()
        bet = int(get_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

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 * .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!')
            exit()

Upvotes: 0

Views: 721

Answers (2)

larsks
larsks

Reputation: 311606

You can certainly exit the loop using an exception; just raise an exception inside your while loop, and catch it outside the loop:

def total_bank(bank):
    bet = 0
    try:
        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): ')
            if get_bet == '0': 
                print('Thanks for playing!')
                raise StopIteration()
            bet = int(get_bet)
    except StopIteration:
        pass
    return bank,bet

But even easier is just to return from inside the loop:

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

Upvotes: 3

Jax
Jax

Reputation: 77

I'm not super knowledgable with the try and except functions but with the try block you could raise an error and end the program like that.

Upvotes: -2

Related Questions