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 trialJolly Mongia
828 PointsAt time stamp 2:47 we move the amount statement to try block. Could you please explain why so ?
We are moving the amount = split_check(total_due,number_of_people) to try block why are we doing so? because when we enter 0 for number_of_people, it is showinf the ValueError("More than one person is required to split the check") error message, then why are we moving that statement to try block?
2 Answers
Alexander Davison
65,469 PointsYou don't have to use exceptions and error handling to write this program, but some programmers like to intentionally raise errors so that they can catch it later. It is a more advanced programming technique, so I wouldn't worry too much about it. I generally don't use error raising/catching too much because it can lead to confusion in the program flow, kind of like using GOTO
in BASIC.
wendy liong
873 Pointswe put it inside the try block because we want to catch the exception as one.
if we had left it in its original place at the "else:" block, we would have produced this: Traceback (most recent call last): File "check_please.py", line 17, in <module> amount_due = split_check(total_due, number_of_people) File "check_please.py", line 5, in split_check raise ValueError("More than 1 person is required to split the check") ValueError: More than 1 person is required to split the check
which is messy and too much information for the user.. as opposed to this:
What is the total? 20 How many people? -1 Oh no! That's not a valid value. Try again... (More than 1 person is required to split the check)