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 trialWilliam Davis
10,070 PointsHow would we deal with users asking for a negative amount of tickets (eg: -1 tickets)?
The exception raised is great for users who want to order more tickets than available and for users who write something other than a positive number, but it doesn't work if I say that I want -1 tickets, as the script continues and even adds it to the number of tickets remaining.
How would we deal with this situation?
2 Answers
Steven Parker
231,269 PointsYou can easily add another test & raise similar to the one for excessive tickets, right above or right below the existing one:
if num_tickets < 1:
raise ValueError("We cannot sell less than 1 ticket!")
For better efficiency, the second test can be changed to an "elif", since both conditions can't be true at the same time.
Pamela Wallace
2,125 PointsThanks, Steve
William Davis
10,070 PointsWilliam Davis
10,070 PointsThank you, Steven.
Pamela Wallace
2,125 PointsPamela Wallace
2,125 PointsWhy doesn't the code below catch this error?
while tickets_remaining >= 1:
Steven Parker
231,269 PointsSteven Parker
231,269 PointsThere could be plenty "tickets_remaining". A negative order shows up in "num_tickets".