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 trialAbdullah Jassim
4,551 Pointsexplanation of video from 2:05
I am having difficulty understanding this video hence please let me know if I am on the right track. I have split my doubts into a series of questions. It would be easier for me to understand if you can respond below each of the questions. Thanks in advance for your help.
1) The objective of raising the ValueError in the split_check function is to catch the error when a user types in a number equal to or less than zero correct?
2) 2:05 - 2:12: So when the video states - split_check(total_due, number_of_people) - is not in the try block and the error becomes unhandled, this means we have ANOTHER objective of capturing a ValueError when processing the split_check function? Is that correct?
3) 2:25: When the error message located in the function pops up, is the raise ValueError working?
3a) If the raise ValueError is working, why move the - split_check(total_due, number_of_people) - below the try block? Because the video says by moving - split_check(total_due, number_of_people) - it will now get caught. I assume its already working because the Error message pops up.
4) By moving - split_check(total_due, number_of_people) - to below the try block, we dont even see the message - "More than 1 person...". This is the part where it gets confusing. If the whole point of moving - split_check(total_due, number_of_people) - below the try block was to capture it, but doing so is NOT capturing it, why move it in the first place?
5) 3:16: Now I assume that the raise exception in the function message is being captured but the message - "more than 1 person..." is not appearing for some reason correct? So for the message to appear you use a reference called err correct?
Apologies for asking too many questions but I want to understand the logic behind each code so that I can use them in the right circumstances.
1 Answer
Murilo de Melo
14,456 PointsAs I understood the code: The program starts running inside the try block
Steps:
"What is the total?" Your input is stored in the variable total_due.
"How many people?" Your input is stored in the variable number_of_people.
Your inputs are then passed as arguments in the statement amount_due = split_check(total_due, number_of_people). , calling the function.
Now, inside the function a check is made for number_of_people, which should raise an exception if it matches the condition, that is, the value of number_of_people you input before is less or equal to 1.
The interpreter will search in the code where the exception handler for ValueError (in a except block) is.
The interpreter stores the message "More than one person is required to split the check" in the variable 'err' as stated with the keyword 'as'.
The order of message display inside the exception handler is:
i. print: "Oh, no! That's not a valid value. Try again..."
ii. print err, that is, the message that was established for this exception inside the function, "More than one person is required to split the check", stored inside 'err'.
Program finishes.
The statement which calls the function, amount_due = split_check(total_due, number_of_people), was before inside the else block. What happened is that the code run from the try block direct to else, ignoring the except ValueError block, because inside the try bock there was not any code that would tell that 0 is invalid. Getting in the else block, the function was called and your inputs passed as arguments. Then, the check statement, if number_of_people <= 1, matched because your input for number_of_people was 0, and then displayed "ValueError: More than one person is required to split the check" in the console, what is not the intended. In my opinion that happened because the caller was inside else and not inside try. If the code is not inside the try block, it is not required to be handled by an except code block, and then it just stopped there inside the function.
Now, moving the caller amount_due = split_check(total_due, number_of_people) to inside the block try, it will be checked inside the function, and if an exception is thrown (less than or equal to 1), display the message set in the raise statement and send it to the block except to be handled.
I don't if this helps a little and neither if it's correct, but this is what I could get from that. Someone may correct if I'm wrong or give more details if it does not answer your questions.
Best regards
Murilo