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.
PROFESSOR: The code looks a little bit daunting,
but it's not that bad.
Really the only part of the code that actually does something
is this part right here, where we're
trying to do these three lines.
The first being, we get an input from the user that
says how old are you, we're converting it to an integer.
We're going to take that number that they gave us,
divide by 80.
I'm assuming life expectancy is 80,
and multiply by 100 to get the percent, going around it to 1.
And then I'm going to print, you've
gone through that much percent of your life.
OK, 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.
And then we're going to also catch
the zero division error which says print
trying to do a division by 0.
And otherwise, we'll have a last except that,
so 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?
OK, so we're giving string, and strings taken in as input
and then trying to 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, 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 0 times 100 divide by 80, that's fine,
it's always going to be zero.
We're going around 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.
So that's this one here.
So the zero division error only happens
when you're trying to divide something by zero.
In this case we're not, the zero's
at the top of the fraction.