Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTerry Felder
1,738 Pointsif 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
231,248 PointsA 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.