Python Python Basics All Together Now Handle Exceptions

if statement vs. raise ValueError

so i went and tried to solve the errors before i got to finish the video and i ended up using an if statement instead of the raise ValueError route.

my question is...what is the difference between using these two methods?

finally if a get the same result using both methods does it matter which i use and why?

p.s once i find a way to format it i will do so ASAP

 try:
        num_tickets = int(input('Okay, {}! How many tickets would you like?: '.format(name)))
        if num_tickets > ticket_remaining:
            print('You exceeded the amount of tickets, there are {} tickets left.'.format(ticket_remaining))
            continue
        else:
            ticket_price = num_tickets * TICKET_PRICE
            print('Okay {} you asked for {} tickets, the total cost will be {}$'.format(name,num_tickets,ticket_price))
    except ValueError:
        print('Invalid key please try to use a number')    
    else:

1 Answer

Steven Parker
Steven Parker
170,254 Points

A similar question was asked two days ago. You might want to take a look at the answers there.

But the main difference is that the instructor's approach yeids slightly more compact code and a more generic error message that displays information from the system.