1 00:00:01,440 --> 00:00:03,370 Hello, and welcome to yet another worked 2 00:00:03,370 --> 00:00:07,450 exercise for Python for Informatics: Exploring Information. 3 00:00:07,450 --> 00:00:11,360 I'm your exercise worker Charles Severance, I'm the author of the book. 4 00:00:11,360 --> 00:00:13,300 And as always, this material, as well 5 00:00:13,300 --> 00:00:15,960 as the book itself, is Creative Commons attribution. 6 00:00:17,830 --> 00:00:20,950 So this exercise assumes that you sort of know 7 00:00:20,950 --> 00:00:23,830 how to edit files and have things set up. 8 00:00:23,830 --> 00:00:25,630 If you if you need more help, go 9 00:00:25,630 --> 00:00:30,210 with www.pythonlearn.com and go through the installation process. 10 00:00:30,210 --> 00:00:34,030 So the exercise we're contemplating right now is Exercise 5.1. 11 00:00:34,030 --> 00:00:37,290 We're going to read a list of numbers. 12 00:00:37,290 --> 00:00:40,960 And deal with some bad data and ultimately 13 00:00:40,960 --> 00:00:43,280 compute the average and finish when we're done. 14 00:00:44,920 --> 00:00:50,290 And so, we're going to, we're going to do this stepwise like we always do. 15 00:00:50,290 --> 00:00:52,500 We're not just going to write this and try 16 00:00:52,500 --> 00:00:55,400 to, you know, imagine what the ultimate thing is. 17 00:00:55,400 --> 00:01:00,020 So the first thing that I'm going to try to, to write, is. 18 00:01:01,660 --> 00:01:05,650 So the first thing I'm going to try to write is entr, enter a number, 19 00:01:05,650 --> 00:01:08,330 convert the number to a floating point, and then print it out, just for you. 20 00:01:08,330 --> 00:01:12,016 So go ahead and get started on that. 21 00:01:12,016 --> 00:01:12,890 Keep the copy that. 22 00:01:12,890 --> 00:01:14,870 I've got a nice copy on paper here. 23 00:01:14,870 --> 00:01:17,470 I'll refer back to my specifications. 24 00:01:17,470 --> 00:01:19,980 So here I have TextWrangler up. 25 00:01:21,480 --> 00:01:29,130 I will go, go into my desktop, I got this folder called Python for Informatics here. 26 00:01:29,130 --> 00:01:30,390 So I go on to my desktop, 27 00:01:32,980 --> 00:01:37,296 Desktop, cd Python for Informatics. 28 00:01:37,296 --> 00:01:39,240 I'll to do an ls, and here's the, 29 00:01:39,240 --> 00:01:44,850 some of the other exercises that I played with before. 30 00:01:44,850 --> 00:01:48,886 So I'm going to make a new file. 31 00:01:48,886 --> 00:01:51,960 I'll just say print hello. 32 00:01:53,170 --> 00:01:55,730 Just yeah, I don't know, I always start this way. 33 00:01:55,730 --> 00:01:56,790 That's how I do it. 34 00:01:58,180 --> 00:01:59,700 Save As. 35 00:01:59,700 --> 00:02:03,600 I'm just making sure I'm already in that, so I come in the right folder. 36 00:02:03,600 --> 00:02:06,606 And I'll call this one numlist. 37 00:02:06,606 --> 00:02:10,400 [BLANK_AUDIO] 38 00:02:10,400 --> 00:02:12,642 dot p-y 39 00:02:12,642 --> 00:02:16,486 If all goes well, you'll notice now it has the Python suffix, it's properly colored, 40 00:02:16,486 --> 00:02:19,214 and son of a gun if I'm not in the correct directory. 41 00:02:19,214 --> 00:02:25,560 So let's go ahead and run this thing numlist.py, hello. 42 00:02:25,560 --> 00:02:27,710 So it's good stuff. 43 00:02:27,710 --> 00:02:28,610 I'll make this a little smaller. 44 00:02:30,690 --> 00:02:31,240 So there we go. 45 00:02:33,020 --> 00:02:34,840 So let's get started. 46 00:02:34,840 --> 00:02:37,088 So instead of saying hello, I'm going to just say 47 00:02:38,088 --> 00:02:41,484 I'm going to get a string variable, in and say 48 00:02:41,484 --> 00:02:51,530 raw input, enter a number, colon. 49 00:02:51,530 --> 00:02:54,390 Then I'm going to add a space just to make it look a little prettier. 50 00:02:54,390 --> 00:02:57,470 Then I'm going to convert that to a floating point number. 51 00:02:57,470 --> 00:02:58,870 I'll call this thing num. 52 00:02:58,870 --> 00:03:03,720 And I'll call float, because inp is a string. 53 00:03:03,720 --> 00:03:08,270 And I want an integer number, floating point numbers so I can add them up. 54 00:03:08,270 --> 00:03:11,290 And I'm just going to print num out, just to sort of be cool. 55 00:03:11,290 --> 00:03:15,980 So I'm going to, on my Mac I can use a Command S instead of all the clicky stuff. 56 00:03:15,980 --> 00:03:17,550 So Command S. 57 00:03:17,550 --> 00:03:18,320 Now I'm in my terminal. 58 00:03:18,320 --> 00:03:21,537 I hit up arrow, so I can run it again. 59 00:03:21,537 --> 00:03:24,511 Enter a number, 79. 60 00:03:24,511 --> 00:03:26,400 That's looking pretty good. 61 00:03:26,400 --> 00:03:28,355 Enter a number. 62 00:03:28,355 --> 00:03:30,385 That's looking pretty bad, but frankly 63 00:03:30,385 --> 00:03:32,780 that's expect, exactly what we would expect. 64 00:03:32,780 --> 00:03:36,240 If we're going to type garbage in, we're going to get a traceback. 65 00:03:36,240 --> 00:03:38,482 We'll deal with that in a minute. 66 00:03:38,482 --> 00:03:39,323 Okay, so. 67 00:03:39,323 --> 00:03:45,030 [SOUND] So it looks like that part's pretty cool. 68 00:03:45,030 --> 00:03:49,835 So our problem now is we want to take this no, let's, let's put that back in. 69 00:03:49,835 --> 00:03:53,900 Let's put this, let's put this in a loop. 70 00:03:53,900 --> 00:03:54,890 So how do we make a loop? 71 00:03:54,890 --> 00:03:57,950 Well, I'll just make an infinite loop for 72 00:03:57,950 --> 00:04:01,380 now, and I'm going to indent these four spaces. 73 00:04:03,850 --> 00:04:05,880 Make sure you have spaces turned, 74 00:04:07,750 --> 00:04:14,374 I mean tabs turned into spaces and it will all just be 75 00:04:14,374 --> 00:04:21,260 [SOUND], and so I'm going to write this loop. 76 00:04:21,260 --> 00:04:23,760 Right now we're writing an infinite loop because it's always going to be true. 77 00:04:24,998 --> 00:04:28,783 So let's go ahead and save this one. 78 00:04:30,511 --> 00:04:38,726 python numlist.py. 79 00:04:38,726 --> 00:04:41,670 1-2-3; 4-5-6, that's another number. 80 00:04:41,670 --> 00:04:44,460 Now we're supposed to be able type done. 81 00:04:47,030 --> 00:04:50,234 Well, our program ended because the float 82 00:04:50,234 --> 00:04:54,221 crashed, right? The float crashed on line three. 83 00:04:54,221 --> 00:04:55,910 And so at least we killed our program. 84 00:04:57,160 --> 00:04:58,120 Because we were in an infinite loop. 85 00:04:58,120 --> 00:04:59,400 It would have been prompting us for 86 00:04:59,400 --> 00:05:01,860 numbers forever, it would have never been done. 87 00:05:01,860 --> 00:05:03,350 So the next thing we're going to need to do 88 00:05:03,350 --> 00:05:05,350 is, we're going to need to get out of the loop. 89 00:05:05,350 --> 00:05:06,510 Okay? 90 00:05:06,510 --> 00:05:09,110 So I'm going to, I'm going to get out of the 91 00:05:09,110 --> 00:05:14,800 loop by checking to see if inp is equal to done, 92 00:05:17,320 --> 00:05:17,820 break. 93 00:05:19,380 --> 00:05:25,170 So all that just basically says if the input the user types, I'm going to break out. 94 00:05:27,120 --> 00:05:33,280 So let's run this again. 3, done. 95 00:05:33,770 --> 00:05:38,130 Now at least it runs and it, it gets out of the of the loop. The break 96 00:05:38,130 --> 00:05:39,600 comes down to here. 97 00:05:39,600 --> 00:05:41,030 That much is pretty good. 98 00:05:41,030 --> 00:05:43,760 Let me add a quick feature here that's not in the specification list. 99 00:05:46,720 --> 00:05:49,134 We'll see if we get in trouble for this one. 100 00:05:49,134 --> 00:05:54,680 I'm going to say if len of inp less than one, break. 101 00:05:55,680 --> 00:05:58,636 This is actually kind of an idiom in here. 102 00:05:58,636 --> 00:06:04,020 Check for empty line. 103 00:06:04,020 --> 00:06:05,410 It's a bit of an idiom. 104 00:06:05,410 --> 00:06:08,430 So, so basically, maybe the poor user would not 105 00:06:08,430 --> 00:06:12,340 think to type done, because they didn't read the specifications. 106 00:06:12,340 --> 00:06:14,200 So we'll save that one. 107 00:06:14,200 --> 00:06:19,510 And you will see now that I can [SOUND]. 108 00:06:19,510 --> 00:06:22,370 just hit Enter key, and it sees that as done as well. 109 00:06:22,370 --> 00:06:25,700 That's probably a more clever way to do it, and a more user-friendly way to do it. 110 00:06:28,510 --> 00:06:30,700 Or maybe it's a violation of specifications, 111 00:06:30,700 --> 00:06:32,310 but we're just going to treat that as [UNKNOWN]. 112 00:06:32,310 --> 00:06:33,570 We'll leave it in there for now. 113 00:06:33,570 --> 00:06:38,410 But that's kind of Python idiom, that you know, ending on a empty line. 114 00:06:38,410 --> 00:06:40,410 Just because it's natural for the user to 115 00:06:40,410 --> 00:06:43,480 just hit Enter, and think is this thing is going to stop? 116 00:06:43,480 --> 00:06:43,880 Okay. 117 00:06:43,880 --> 00:06:46,820 So now our problem is, is we've got to count these things. 118 00:06:46,820 --> 00:06:50,260 So we've got this loop body, and it's running a bunch of times. 119 00:06:50,260 --> 00:06:52,890 And we want to count how many times it runs. 120 00:06:52,890 --> 00:06:56,540 So we'll make a variable called count, set it to zero. 121 00:06:56,540 --> 00:07:02,427 And then each time we come through we're going to add count equals count plus 1. 122 00:07:04,160 --> 00:07:05,200 Now, oops. 123 00:07:08,890 --> 00:07:11,126 Now, I did not put the count here. 124 00:07:19,890 --> 00:07:24,190 And that's because I only want to count the numbers that I've actually read. 125 00:07:24,190 --> 00:07:26,390 So I'm, I've read a number. 126 00:07:26,390 --> 00:07:32,750 Because if I, if I increment my count here and I break, it'll be one too many. 127 00:07:32,750 --> 00:07:33,890 Well let's just go ahead and do that. 128 00:07:35,100 --> 00:07:37,190 Just say count equals and make that mistake. 129 00:07:38,840 --> 00:07:39,282 Right? 130 00:07:39,282 --> 00:07:43,240 So I'm going to, going to get rid of this, and I'll put it here. 131 00:07:43,240 --> 00:07:44,840 Exactly where I say don't count here. 132 00:07:46,660 --> 00:07:47,958 Right? 133 00:07:47,958 --> 00:07:50,340 So I'm going to count here, and this is a bad place to count. 134 00:07:50,340 --> 00:07:53,778 So I'm going to save that, and this loop is going to go until it's done 135 00:07:53,778 --> 00:07:56,010 and then it's going to print out how many numbers we've got. 136 00:07:56,010 --> 00:08:03,210 [SOUND] So you see here, it's got a mistake. 137 00:08:03,210 --> 00:08:05,630 It's not a syntax error, it's a logic error. 138 00:08:05,630 --> 00:08:09,070 Because we've got thr count up to 3, but that's because 139 00:08:09,070 --> 00:08:12,830 the last time through the code, you know, count was zero. 140 00:08:12,830 --> 00:08:15,280 We read a number, we added one to the count. 141 00:08:15,280 --> 00:08:18,690 We read another number, added one to the count. 142 00:08:18,690 --> 00:08:22,130 And then we read another bad number, we added a number to the count, 143 00:08:22,130 --> 00:08:24,300 and then we end up with this break. 144 00:08:24,300 --> 00:08:26,225 So the count is wrong, basically. 145 00:08:26,225 --> 00:08:27,680 Okay? 146 00:08:27,680 --> 00:08:30,850 And so, the key is that we've got to move 147 00:08:31,910 --> 00:08:34,080 we've got to move the count down. 148 00:08:34,080 --> 00:08:37,900 We only want to count the valid data, we don't want to count invalid data. 149 00:08:37,900 --> 00:08:41,860 So what we were doing is we were counting the last non-data bit. 150 00:08:41,860 --> 00:08:44,895 And so it's just, that's just, you know, another thing to look for. 151 00:08:44,895 --> 00:08:51,565 You know, the, the pattern here is, you know, read your data, 152 00:08:51,565 --> 00:08:55,708 decide if you've got bad data or good data, or you want to stop, or whatever. 153 00:08:55,708 --> 00:08:57,478 And then do the work. 154 00:08:57,478 --> 00:09:07,042 So I'll put a comment in here called do, oop, do the work. 155 00:09:07,042 --> 00:09:16,304 So this is the work, and this is kind of handle the edge cases. 156 00:09:16,304 --> 00:09:17,957 Right? 157 00:09:17,957 --> 00:09:21,270 And this is get the data, handle the edge cases, then do the work. 158 00:09:21,270 --> 00:09:24,180 And incrementing count is just part of the work. 159 00:09:24,180 --> 00:09:25,560 Okay? 160 00:09:25,560 --> 00:09:27,510 So let's keep going. 161 00:09:29,200 --> 00:09:32,100 Now we've gotta make our, our average. 162 00:09:32,100 --> 00:09:34,380 So average of course be the total. 163 00:09:37,260 --> 00:09:42,654 And that's pretty easy, because we're going to 164 00:09:42,654 --> 00:09:49,640 say total equals total plus num. 165 00:09:49,640 --> 00:09:50,990 Simple enough? 166 00:09:50,990 --> 00:09:57,885 So I'm going to print out num, total, and count. 167 00:09:57,885 --> 00:09:58,160 Okay? 168 00:09:58,160 --> 00:09:58,660 So we'll. 169 00:09:59,700 --> 00:10:03,140 So now 12, 14. 170 00:10:03,140 --> 00:10:09,320 So you see our number is 12, our total is 12, and our count's 1. 171 00:10:09,320 --> 00:10:11,960 Number's 14, total 26, count of 2. 172 00:10:11,960 --> 00:10:12,740 Looking pretty good. 173 00:10:14,360 --> 00:10:17,610 Let's just hit Enter, and the count is 2. 174 00:10:17,610 --> 00:10:19,780 So the only, so we got count. 175 00:10:19,780 --> 00:10:24,240 So, let's change this to be what we're suppose to change it to. 176 00:10:24,240 --> 00:10:25,120 Say average. 177 00:10:29,500 --> 00:10:33,215 colon, then no space, because the comma adds a space, 178 00:10:33,215 --> 00:10:40,938 and we're going to just say total over count. 179 00:10:40,938 --> 00:10:45,200 So let's run that. 180 00:10:45,200 --> 00:10:48,254 Run 10, 11, 10.5. 181 00:10:48,254 --> 00:10:49,957 So our numbers are working. 182 00:10:49,957 --> 00:10:51,300 So we're getting pretty close. 183 00:10:51,300 --> 00:10:55,950 We really have one more problem to solve and that is [SOUND] that problem. 184 00:10:55,950 --> 00:10:57,940 The problem when we have crappy data. 185 00:10:57,940 --> 00:11:00,456 And we don't want to use traceback as 186 00:11:00,456 --> 00:11:03,788 our mechanism, we just want to re-prompt the user. 187 00:11:05,220 --> 00:11:08,500 So the key thing when you're dealing with this is, the first 188 00:11:08,500 --> 00:11:11,550 thing to take a look at is the actual line in question. 189 00:11:11,550 --> 00:11:15,250 The line that caused the error, line that caused the error 190 00:11:15,250 --> 00:11:20,660 is line 11, and it's this line right here. 191 00:11:20,660 --> 00:11:24,300 So, we can't control what the user types. 192 00:11:24,300 --> 00:11:26,660 The simplest thing to do is to use the try-except 193 00:11:29,470 --> 00:11:32,120 to put, take out some insurance on this particular line. 194 00:11:32,120 --> 00:11:33,690 It's like, that's a dangerous line. 195 00:11:35,620 --> 00:11:38,850 And then except tells us what to do. 196 00:11:38,850 --> 00:11:41,790 So now the question is, well let's put out a print statement. 197 00:11:43,950 --> 00:11:45,020 All right. 198 00:11:45,020 --> 00:11:46,680 And print what we're supposed to say. 199 00:11:46,680 --> 00:11:48,580 Invalid input. 200 00:11:51,150 --> 00:11:51,540 Okay? 201 00:11:51,540 --> 00:11:53,240 So here we go. 202 00:11:53,240 --> 00:11:58,310 We've got invalid input, and so we've got an exception. 203 00:11:58,310 --> 00:12:00,820 You know, I've made a mistake on purpose. 204 00:12:00,820 --> 00:12:03,250 You can pause and try to figure out what the mistake is, but. 205 00:12:04,780 --> 00:12:06,420 Okay, you got your chance to pause, so let's 206 00:12:06,420 --> 00:12:07,930 go take a look at what the mistake is. 207 00:12:07,930 --> 00:12:11,350 Let's come up with some evil data that causes the problem. 208 00:12:11,350 --> 00:12:16,600 So 10 is our first number. 209 00:12:17,600 --> 00:12:20,920 12 is our second number. 210 00:12:23,420 --> 00:12:26,060 And now I'm going to say fred. 211 00:12:28,430 --> 00:12:30,460 So now you should be able to see what's wrong here. 212 00:12:32,830 --> 00:12:36,320 The problem is that basically fred indeed says invalid input. 213 00:12:36,320 --> 00:12:40,940 That works because it runs down this line of code and it says invalid input. 214 00:12:40,940 --> 00:12:44,840 The problem is, it's still executing this code right here. 215 00:12:44,840 --> 00:12:48,560 So, the number actually the float failed. 216 00:12:48,560 --> 00:12:50,570 We said fred but the number's 12 and that's 217 00:12:50,570 --> 00:12:53,660 because it's 12 from the last time through the loop. 218 00:12:53,660 --> 00:12:56,220 The last time through the loop, okay? 219 00:12:56,220 --> 00:13:00,020 So it ends up adding 12 again, 22, then we have three numbers and 220 00:13:00,020 --> 00:13:05,240 then we finish, oops, that wa in the wrong window there, and then when we finish, 221 00:13:05,240 --> 00:13:12,776 the average is not 11, which is 10 plus 12, but it is indeed 11.3. 222 00:13:12,776 --> 00:13:19,220 So the problem is when we have this exception, we can't run this code. 223 00:13:19,220 --> 00:13:25,760 And of course there's a lovely, lovely bit of Python to fix this, solves this for us. 224 00:13:25,760 --> 00:13:27,770 And that is the continue statement. 225 00:13:27,770 --> 00:13:30,620 Continue statement says, I'm done with this iteration. 226 00:13:31,620 --> 00:13:32,860 You entered a number. 227 00:13:32,860 --> 00:13:35,150 It's crappy, I'm just not going to do anything. 228 00:13:35,150 --> 00:13:39,270 So effectively instead of going down here to continue, 229 00:13:39,270 --> 00:13:41,900 it goes up to here and prompts for the next one. 230 00:13:41,900 --> 00:13:47,980 So it doesn't do this code, right? It does not do that code, and everything is cool. 231 00:13:47,980 --> 00:13:48,790 And so that's the key. 232 00:13:48,790 --> 00:13:51,836 When we say invalid input, we do not want to run this code. 233 00:13:51,836 --> 00:13:58,410 Oops [SOUND] so let's save this. 234 00:13:58,410 --> 00:14:00,780 So continue's our friend on this one. 235 00:14:05,110 --> 00:14:07,551 So 10, 12. 236 00:14:07,800 --> 00:14:10,070 I'm doing these just so I can know the average should be 11. 237 00:14:10,070 --> 00:14:15,010 And then fred. Now you'll notice it says invalid input, but because of 238 00:14:15,010 --> 00:14:17,810 continue, it doesn't fall through and do all the rest of that stuff. 239 00:14:19,050 --> 00:14:23,850 And then let's hit Enter to finish and the average is indeed 11.0. 240 00:14:23,850 --> 00:14:25,810 Okay? 241 00:14:25,810 --> 00:14:28,400 So again, I think that's pretty much it. 242 00:14:30,830 --> 00:14:32,650 Thanks for listening. 243 00:14:32,650 --> 00:14:38,560 For more information Python for informatics, www.py4inf.com. 244 00:14:38,560 --> 00:14:38,940 Thanks.