1 00:00:00,790 --> 00:00:03,190 The following content is provided under a Creative 2 00:00:03,190 --> 00:00:04,730 Commons license. 3 00:00:04,730 --> 00:00:07,030 Your support will help MIT OpenCourseWare 4 00:00:07,030 --> 00:00:11,390 continue to offer high quality educational resources for free. 5 00:00:11,390 --> 00:00:13,990 To make a donation or view additional materials 6 00:00:13,990 --> 00:00:17,534 from hundreds of MIT courses, visit MIT OpenCourseWare 7 00:00:17,534 --> 00:00:18,620 at ocw.mit.edu. 8 00:00:25,110 --> 00:00:27,110 PROFESSOR: The code looks a little bit daunting, 9 00:00:27,110 --> 00:00:28,026 but it's not that bad. 10 00:00:28,026 --> 00:00:32,530 Really the only part of the code that actually does something 11 00:00:32,530 --> 00:00:34,030 is this part right here, where we're 12 00:00:34,030 --> 00:00:37,720 trying to do these three lines. 13 00:00:37,720 --> 00:00:41,140 The first being, we get an input from the user that 14 00:00:41,140 --> 00:00:44,380 says how old are you, we're converting it to an integer. 15 00:00:44,380 --> 00:00:49,390 We're going to take that number that they gave us, 16 00:00:49,390 --> 00:00:50,770 divide by 80. 17 00:00:50,770 --> 00:00:53,080 I'm assuming life expectancy is 80, 18 00:00:53,080 --> 00:00:56,482 and multiply by 100 to get the percent, going around it to 1. 19 00:00:56,482 --> 00:00:57,940 And then I'm going to print, you've 20 00:00:57,940 --> 00:01:02,210 gone through that much percent of your life. 21 00:01:02,210 --> 00:01:04,209 OK, so again we're dealing with user input which 22 00:01:04,209 --> 00:01:07,090 is unpredictable, so we're going to catch, first 23 00:01:07,090 --> 00:01:09,040 of all, a value error, and that's 24 00:01:09,040 --> 00:01:10,510 if the user doesn't enter a number. 25 00:01:10,510 --> 00:01:14,080 In that case we're going to say, oops, you must enter a number. 26 00:01:14,080 --> 00:01:15,800 And then we're going to also catch 27 00:01:15,800 --> 00:01:19,300 the zero division error which says print 28 00:01:19,300 --> 00:01:20,980 trying to do a division by 0. 29 00:01:20,980 --> 00:01:23,280 And otherwise, we'll have a last except that, 30 00:01:23,280 --> 00:01:25,240 so something went very wrong. 31 00:01:25,240 --> 00:01:27,040 So looks like people are getting it right. 32 00:01:27,040 --> 00:01:29,140 The question being, if the user enters 33 00:01:29,140 --> 00:01:32,180 20, what does the program do? 34 00:01:32,180 --> 00:01:38,260 OK, so we're giving string, and strings taken in as input 35 00:01:38,260 --> 00:01:40,540 and then trying to convert those to an integer 36 00:01:40,540 --> 00:01:42,670 gives us a value error. 37 00:01:42,670 --> 00:01:44,620 So it's going to go through this except block 38 00:01:44,620 --> 00:01:48,967 here and print that, which is great, most of you guys 39 00:01:48,967 --> 00:01:49,925 are getting that right. 40 00:01:52,860 --> 00:01:57,750 And then the next one, perfect, I tricked some of you. 41 00:01:57,750 --> 00:01:59,990 The next one says, if the user enters 42 00:01:59,990 --> 00:02:02,220 zero, what does the program do? 43 00:02:05,070 --> 00:02:06,623 Well what does the program do? 44 00:02:06,623 --> 00:02:08,039 We're going to take in zero, we're 45 00:02:08,039 --> 00:02:11,490 going to do 0 times 100 divide by 80, that's fine, 46 00:02:11,490 --> 00:02:12,840 it's always going to be zero. 47 00:02:12,840 --> 00:02:15,740 We're going around that to one, and then we're 48 00:02:15,740 --> 00:02:18,120 going to print that out. 49 00:02:18,120 --> 00:02:20,900 So no zero division error, no value error, 50 00:02:20,900 --> 00:02:23,960 everything went right, so that means 51 00:02:23,960 --> 00:02:25,644 it's going to print out this. 52 00:02:25,644 --> 00:02:28,370 The first one. 53 00:02:28,370 --> 00:02:30,840 So that's this one here. 54 00:02:30,840 --> 00:02:32,660 So the zero division error only happens 55 00:02:32,660 --> 00:02:35,750 when you're trying to divide something by zero. 56 00:02:35,750 --> 00:02:37,490 In this case we're not, the zero's 57 00:02:37,490 --> 00:02:39,640 at the top of the fraction.