# Exceptions

**Channel:** MIT OpenCourseWare
**Source:** https://www.youtube.com/watch?v=0Whyfs88TYE
**Transcript page:** https://www.withtranscript.ai/video/0Whyfs88TYE

## Chapters

- 0:00 — Chapter 1 (0:00)

## Transcript

**[0:00]:** The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseware continue to offer high-quality educational resources for free. To make a donation or view additional materials from hundreds of MIT courses, visit MIT OpenCourseware at ocw.mit.edu.

**[0:23]:** Okay. Code looks a little bit daunting, but it's not that bad. Really the only part of the code that actually does something is is this part right here where we're trying to do this these three lines. The first being we get an input from the user that says, "How old are you?" We're converting that to an integer. We're going to take that number that they gave you uh that they gave us, divide by 80. I'm assuming life expect- expectancy is 80 and multiply by 100 to get the percent. Going to round it to one. And then I'm going to print, "You've gone through that much percent of your life." Okay. So again, we're dealing with user input, which is unpredictable. So we're going to catch first of all a value error. And that's if the user doesn't enter a number. In that case, we're going to say, "Oops, you must enter a number."

**[1:14]:** And then we're going to also catch the zero division error, which says print divided by uh trying to do a division by zero. And otherwise, we're have a last except that says something went very wrong. So looks like people are getting it right. The question being, if the user enters 20, what does the program do? Okay, so we're giving uh string. And strings taken in as input and then trying to convert convert those to an integer gives us a value error. So it's going to go through this except block here and print that, which is great. Most of you guys are getting that right. And then the next one, ah, perfect. I tricked some of you. The next one says, "If the user enters zero, what does the program do?" Well, what does the program do? We're going to take in zero, we're going to do zero times 100 / 80.

**[2:10]:** That's fine. It's always going to be zero. Going to round that to one. And then we're going to print that out. So, no zero division error, no value error, everything went right. So, that means it's going to print out this. The first one. Okay. So, that's this one here. So, the zero division error only happens when you're trying to divide something by zero, right? In this case, we're not. The zero's uh at the top of the fraction.
