1 00:00:00,170 --> 00:00:04,880 Hello, and welcome to Chapter Three of Python for Informatics. 2 00:00:04,880 --> 00:00:06,910 Chapter One, Chapter Two, now we're 3 00:00:06,910 --> 00:00:08,640 going to get to something kind of programmy. 4 00:00:08,640 --> 00:00:15,650 I mean, assignment statements and reserved words, that just kind of gurgling. 5 00:00:15,650 --> 00:00:18,030 Now we're going to start seeing composition. 6 00:00:18,030 --> 00:00:20,563 We're going to start seeing the conditional execution. 7 00:00:20,563 --> 00:00:22,770 Gets us started, sort of, seeing the power 8 00:00:22,770 --> 00:00:25,520 of computers, where you're starting to make decisions. 9 00:00:25,520 --> 00:00:31,320 So, as always, this lecture and audio, video, and slides are also available, 10 00:00:31,320 --> 00:00:34,870 are copyright, Creative Commons Attribution. 11 00:00:34,870 --> 00:00:40,350 So, conditional steps are steps that may or may not be executed. 12 00:00:40,350 --> 00:00:42,550 So here's, here's a bit of code. 13 00:00:42,550 --> 00:00:45,930 So, and, and I draw these pictures. I won't 14 00:00:45,930 --> 00:00:48,070 draw too many of these pictures on the left-hand side. 15 00:00:48,070 --> 00:00:50,750 If you've taken a programming class, you may have seen these. 16 00:00:50,750 --> 00:00:53,020 They're sometimes called flow charts. 17 00:00:53,020 --> 00:00:55,165 Sometimes people really think these are important. 18 00:00:55,165 --> 00:00:58,520 I don't think they're all that important for understanding. 19 00:00:58,520 --> 00:01:00,144 The, the Python code is here on the 20 00:01:00,144 --> 00:01:03,227 right-hand side, and this picture's on the left-hand side. 21 00:01:03,227 --> 00:01:08,407 And, and the reality is is that this may, initially, make more sense, cognitively, 22 00:01:08,407 --> 00:01:13,710 to you, than this. But this part on the right-hand side is what's important. 23 00:01:13,710 --> 00:01:15,840 I like to call these like road maps, so you can sort of 24 00:01:15,840 --> 00:01:19,600 trace where the code is going by driving down a little road. 25 00:01:19,600 --> 00:01:22,030 That's kind of a, something that you do once or 26 00:01:22,030 --> 00:01:24,610 twice and then, pretty soon, you'll start reading the code. 27 00:01:24,610 --> 00:01:25,980 So I'm going to start on the right-hand 28 00:01:25,980 --> 00:01:28,770 side here, and just walk through the code. 29 00:01:28,770 --> 00:01:30,890 Remember, code operates in sequence. 30 00:01:30,890 --> 00:01:36,670 Well, there is a if, which is a special reserved word. 31 00:01:36,670 --> 00:01:39,920 It's one of those things that you can't, you can't name a variable if. 32 00:01:41,190 --> 00:01:45,483 And it is our indication that to Python, that the next 33 00:01:45,483 --> 00:01:50,018 statement that we're going to do may or may not be executed, if. 34 00:01:50,018 --> 00:01:52,874 And the thing that comes on the same line as the if, 35 00:01:52,874 --> 00:01:57,130 up to and including the, the little colon, is a question. 36 00:01:57,130 --> 00:01:58,940 This is a question. 37 00:01:58,940 --> 00:02:01,050 You're asking a question. 38 00:02:01,050 --> 00:02:05,360 So an assignment statement is moving a value into a variable. 39 00:02:05,360 --> 00:02:06,440 And a if statement 40 00:02:06,440 --> 00:02:07,826 is asking a question. 41 00:02:07,826 --> 00:02:10,280 In this case, we're asking a question about a variable. 42 00:02:10,280 --> 00:02:15,890 So always think, when you're, sort of, here, that this is a question to be asked. 43 00:02:15,890 --> 00:02:17,828 And, you'll notice when I'm doing the same 44 00:02:17,828 --> 00:02:19,889 thing over here, I put a question mark there. 45 00:02:19,889 --> 00:02:22,717 Is x less than 10? Yes or no? 46 00:02:22,717 --> 00:02:24,780 It's a question that has a yes or no. 47 00:02:24,780 --> 00:02:27,820 And so, the way this works is, this statement 48 00:02:27,820 --> 00:02:31,640 that's indented, after the if, is either executed or 49 00:02:31,640 --> 00:02:34,490 not executed based on the result of that question. 50 00:02:34,490 --> 00:02:38,110 So the way to sort of read this in English is set x to 5. 51 00:02:38,110 --> 00:02:43,880 If x is less than 10, which it is because x is 5, then we're going to execute this. 52 00:02:43,880 --> 00:02:45,710 So print Smaller comes out. 53 00:02:45,710 --> 00:02:48,626 And then we come back out and we continue and say, oh, okay, now 54 00:02:48,626 --> 00:02:52,068 I have another if statement, and then a bit of, a block of indented code. 55 00:02:52,068 --> 00:02:55,551 If x is less than 20, that's the question. 56 00:02:55,551 --> 00:02:56,766 The answer to that 57 00:02:56,766 --> 00:03:00,747 is no, and so it does not run that line, and so it runs Finis. 58 00:03:00,747 --> 00:03:04,823 So the printout of this program is Smaller, followed by Finis. 59 00:03:04,823 --> 00:03:11,115 What happens is, this line never executes because the answer 60 00:03:11,115 --> 00:03:16,863 to this question is false. Okay? So, let's go through that a little faster. 61 00:03:16,863 --> 00:03:20,581 Set x to five. If x is less than 10, print Smaller. 62 00:03:20,581 --> 00:03:21,895 Then, if x is greater 63 00:03:21,895 --> 00:03:25,843 than 20, which it's not, skip that, and then print Finis. 64 00:03:25,843 --> 00:03:28,160 That's the short version of it, okay? 65 00:03:28,160 --> 00:03:29,630 Conditional steps. 66 00:03:29,630 --> 00:03:32,040 This step is conditional, this step is conditional. 67 00:03:32,040 --> 00:03:36,180 They may or may not be executed based on the result of the question. 68 00:03:36,180 --> 00:03:38,850 Now, if we're thinking of this as like a GPS 69 00:03:38,850 --> 00:03:41,860 road map or something, we can look at this right-hand side. 70 00:03:41,860 --> 00:03:46,810 So, the CPU comes roaring down here, x equals 5, okay, I'll run that. 71 00:03:46,810 --> 00:03:52,240 Then it's faced with a choice. Do, is x less than 10, yes or no? 72 00:03:52,240 --> 00:03:55,700 If it is yes, and it is, I will go this way. 73 00:03:55,700 --> 00:03:57,630 If it was no, I would go that way. 74 00:03:57,630 --> 00:04:00,860 So if it's yes, I go here and I run this little thing and I print Smaller, great. 75 00:04:00,860 --> 00:04:01,951 And I follow the little road. 76 00:04:01,951 --> 00:04:05,890 And now the road takes me to here. And it's asking another question. 77 00:04:05,890 --> 00:04:07,550 Is x greater than 20? 78 00:04:07,550 --> 00:04:11,520 This time, the answer is no, so I'd come down here, right? 79 00:04:11,520 --> 00:04:12,180 And so, 80 00:04:12,180 --> 00:04:14,970 this bit of code is never executed. 81 00:04:14,970 --> 00:04:20,010 Now, this is a very simple example, but you get the basic idea. 82 00:04:20,010 --> 00:04:22,052 Okay? So that's conditional execution. 83 00:04:22,052 --> 00:04:26,665 Now there's a number of conditional operators that we 84 00:04:26,665 --> 00:04:30,130 want to use, just like we had multiplication, division. 85 00:04:30,130 --> 00:04:34,330 Some of them are pretty intuitive, and the others, you just kind of have 86 00:04:34,330 --> 00:04:37,240 to memorize, like less than and greater than make a lot of sense. 87 00:04:38,320 --> 00:04:42,040 The one that probably, that, easy, like less than or equal to, 88 00:04:42,040 --> 00:04:44,720 or greater than or equal to, those kind of make sense, too. 89 00:04:44,720 --> 00:04:47,288 They're less than or equal to. 90 00:04:47,288 --> 00:04:50,024 Just because we don't have a less than or equal to sign on a 91 00:04:50,024 --> 00:04:53,916 symbol or a greater than or equal sign, which we would use in mathematics. 92 00:04:53,916 --> 00:04:57,206 Equality, asking the question of whether something is 93 00:04:57,206 --> 00:05:00,213 equal to something else or not, is double equal. 94 00:05:00,213 --> 00:05:04,008 And that's because we're already using single equals as assignment. 95 00:05:04,008 --> 00:05:10,120 So when we say x equals 3, that is an assignment and sticks a value into x. 96 00:05:10,120 --> 00:05:12,320 This is the question. 97 00:05:12,320 --> 00:05:14,290 Is x equal to? 98 00:05:14,290 --> 00:05:15,760 If I was building a language, I would make 99 00:05:15,760 --> 00:05:18,800 it be equal question mark, or something like that. 100 00:05:18,800 --> 00:05:21,770 I'd be like, huh, is it equal? Kind of a question mark. 101 00:05:21,770 --> 00:05:23,390 But that's not what we do. 102 00:05:23,390 --> 00:05:26,280 I didn't invent this, so we're, double equals 103 00:05:26,280 --> 00:05:30,140 is the question, is something equal to another. 104 00:05:30,140 --> 00:05:35,260 Single equals changes something, x equals five changes x. 105 00:05:35,260 --> 00:05:40,250 Okay, and then, not equal, exclamation is commonly used to mean not in 106 00:05:40,250 --> 00:05:44,584 computer contexts, so if something is not equal to something, 107 00:05:44,584 --> 00:05:50,270 it is exclamation equal. Here are some examples. 108 00:05:50,270 --> 00:05:52,059 Just kind of running through them. 109 00:05:52,059 --> 00:05:55,209 They're all, they all turn out to be true, because I set x to 5. 110 00:05:55,209 --> 00:05:58,490 If x equals 5, print Equals 5. 111 00:05:58,490 --> 00:06:02,140 Come out here, if x is greater than 4, which is true, print Greater than 4. 112 00:06:02,140 --> 00:06:04,730 If x greater than or equal to 5, yep. 113 00:06:04,730 --> 00:06:07,590 If x less than 6, print Less than 6. 114 00:06:07,590 --> 00:06:11,580 Now here's a, there are two, sort of, syntaxes to, to the if statement. 115 00:06:11,580 --> 00:06:16,160 One is where the if statement is down here on a separate line and it's indented, 116 00:06:16,160 --> 00:06:20,050 and the other is where there's a single line and it's right on the same line, 117 00:06:20,050 --> 00:06:23,040 if x less than 6, print Less than 6. 118 00:06:23,040 --> 00:06:26,330 So this is true, so this whole thing executes. 119 00:06:26,330 --> 00:06:28,710 Then it continues down, if x less than or equal to 5? 120 00:06:28,710 --> 00:06:30,500 Yep, print Less than or Equal 5. 121 00:06:30,500 --> 00:06:35,220 If x is not equal to 6, which is true, cuz it's 5, then Not equal to 6. 122 00:06:35,220 --> 00:06:39,950 So, all those will turn out to be true, and all those will execute. 123 00:06:39,950 --> 00:06:45,660 And so, the, the tricky bit, here, is, you know, just knowing, 124 00:06:45,660 --> 00:06:50,430 seeing this syntax for an if statement, where it's all one line, and this syntax, 125 00:06:50,430 --> 00:06:53,410 where you end the first line with a colon and then indent the second line. 126 00:06:54,420 --> 00:06:55,710 This, you can only do one line. 127 00:06:55,710 --> 00:06:58,550 We will soon see that you can put more than one line in the indented block. 128 00:06:59,940 --> 00:07:02,395 Okay. 129 00:07:03,395 --> 00:07:05,405 Here we have more than one int line in 130 00:07:05,405 --> 00:07:08,735 the indented block, these are called one-way decisions. 131 00:07:08,735 --> 00:07:10,575 And so, we say x equals 5, 132 00:07:10,575 --> 00:07:13,723 we print out Before 5, so that prints out. 133 00:07:13,723 --> 00:07:19,520 If x equals 5, remember the double equals is the question mark version of equality, 134 00:07:19,520 --> 00:07:21,960 single equals assignment, it says yes. 135 00:07:21,960 --> 00:07:24,550 So we indent, and the convention is to indent four spaces, 136 00:07:24,550 --> 00:07:28,030 although it doesn't really matter as long as you're consistent. 137 00:07:28,030 --> 00:07:29,470 Then it's going to run all three of those. 138 00:07:29,470 --> 00:07:33,250 Is 5, Still 5, Third 5, these lines all come out. 139 00:07:33,250 --> 00:07:34,500 And then it comes out and prints, 140 00:07:35,510 --> 00:07:39,350 and the de-indenting, the fact that this print has been moved to line up with 141 00:07:39,350 --> 00:07:41,770 the if, that's what indicates that this little 142 00:07:41,770 --> 00:07:46,770 block of conditional executed code is finished. 143 00:07:46,770 --> 00:07:52,700 So then prints out Afterwards 5, comes down some more, Before 6, then it asks 144 00:07:52,700 --> 00:07:55,060 another question, if x is equal to 6, 145 00:07:55,060 --> 00:07:57,020 again, that's the question mark version of it. 146 00:07:58,100 --> 00:08:00,610 And if this is false, now, because x 147 00:08:00,610 --> 00:08:01,980 happens to be 5, so the answer 148 00:08:01,980 --> 00:08:05,990 to this expression, the logical expression, is false. 149 00:08:05,990 --> 00:08:10,730 Then it skips all of the indented bits, so none of this executes. 150 00:08:10,730 --> 00:08:13,960 So, since it's false, it skips all of the indented bit, but then it, 151 00:08:13,960 --> 00:08:18,060 this print lines up, and so then it picks back up with Afterwards 6. 152 00:08:18,060 --> 00:08:20,820 So we call this a one-way decision, where you have the question, and then 153 00:08:20,820 --> 00:08:24,180 you have a couple of things that you're going to do on this true, true thing. 154 00:08:24,180 --> 00:08:26,170 Or, if it turns out that you're false, 155 00:08:26,170 --> 00:08:27,590 you're going to skip all those things. 156 00:08:30,208 --> 00:08:33,530 So, Python is actually one of the 157 00:08:33,530 --> 00:08:38,419 few languages that uses indentation as syntactically significant. 158 00:08:39,750 --> 00:08:42,850 We like to indent code to, for ifs, and 159 00:08:42,850 --> 00:08:44,850 in a moment, we'll see you learn about loops. 160 00:08:44,850 --> 00:08:46,260 We like to indent code as a way to 161 00:08:47,480 --> 00:08:50,578 make sense of stuff, it makes it easier to read. 162 00:08:50,578 --> 00:08:54,586 You know, if this thing's inside, and so, it, it's really quite nice. 163 00:08:54,586 --> 00:08:55,198 And then, 164 00:08:55,198 --> 00:08:57,374 we, sort of, use it as a matching, to 165 00:08:57,374 --> 00:09:01,390 help us cognitively understand what's inside of a program. 166 00:09:02,750 --> 00:09:05,540 But in Python, it's really, really important, and it's 167 00:09:05,540 --> 00:09:08,100 almost, it's, it's, you have to think of, like, 168 00:09:08,100 --> 00:09:10,930 when you are moving in, you mean something, 169 00:09:10,930 --> 00:09:13,310 and when you move back out, you mean something. 170 00:09:13,310 --> 00:09:16,170 So you can increase the indent, which you do after, like, 171 00:09:16,170 --> 00:09:18,400 an if statement, or any other statement that ends in a colon. 172 00:09:18,400 --> 00:09:20,530 You increase the indent, and then 173 00:09:20,530 --> 00:09:22,730 when you're done, you decrease the indent. 174 00:09:22,730 --> 00:09:25,140 You maintain the indent, sort of, for sequential code. 175 00:09:26,300 --> 00:09:28,670 Now blank lines and comments are ignored. 176 00:09:28,670 --> 00:09:31,000 So you can have a blank line and it, it, the 177 00:09:31,000 --> 00:09:34,470 indentation just goes right past it and the comments don't affect it. 178 00:09:34,470 --> 00:09:41,639 And so, while we're here, we'll interrupt us for a recommendation. 179 00:09:44,150 --> 00:09:50,030 In your text editor, Notepad Plus or Text Edit or TextWrangler, or whatever 180 00:09:50,030 --> 00:09:55,420 you're using, it may be set, when you hit the tab key, to move in four spaces. 181 00:09:56,720 --> 00:10:00,150 Sometimes you also might move in four spaces by hitting space bar four times. 182 00:10:01,170 --> 00:10:03,570 Python will see that as different. 183 00:10:03,570 --> 00:10:08,910 And it is possible in all of these word processors to say, hey, 184 00:10:08,910 --> 00:10:13,730 don't actually put tabs in my document, when I hit the tab, put in four spaces. 185 00:10:13,730 --> 00:10:17,539 Then, whether you're hitting the space bar or hitting the tab, at least you 186 00:10:17,539 --> 00:10:18,770 are putting the same thing into your 187 00:10:18,770 --> 00:10:21,330 document and don't, not freaking Python out. 188 00:10:22,750 --> 00:10:25,560 If you don't, you may get indentation errors. 189 00:10:25,560 --> 00:10:28,575 Indentation errors are syntax errors to Python. 190 00:10:28,575 --> 00:10:32,070 And what's really frustrating is, if you, it looks good 191 00:10:32,070 --> 00:10:33,810 to you in your text editor, you have an if, 192 00:10:33,810 --> 00:10:36,750 and the block goes in, and comes back out, but one of them is 193 00:10:36,750 --> 00:10:40,600 four spaces and one of them is a tab, then Python will yell at you. 194 00:10:40,600 --> 00:10:43,550 And this is really frustrating, when Python yells at you about that. 195 00:10:45,140 --> 00:10:49,290 So what I'd like you to do is go into your text editor, whatever it is, 196 00:10:50,385 --> 00:10:53,510 into the properties or the settings. 197 00:10:53,510 --> 00:10:59,480 And here is, you know, your, yours may be different, but here is where you set this. 198 00:11:01,640 --> 00:11:06,390 Auto expand tabs, that is on the Mac in TextWrangler, and then, 199 00:11:06,390 --> 00:11:08,730 in Notepad Plus Plus, there is replace 200 00:11:08,730 --> 00:11:11,270 tabs as spaces, and that's underneath preferences. 201 00:11:11,270 --> 00:11:12,710 So you have to find it. 202 00:11:12,710 --> 00:11:17,280 Stop right now, and go set this so you're not going to make yourself crazy. 203 00:11:18,840 --> 00:11:21,620 Okay, so, this is kind of a busy slide, but it gives 204 00:11:21,620 --> 00:11:27,983 you the sense that you have to explicitly think about indenting and de-indenting. 205 00:11:27,983 --> 00:11:29,814 Okay? And so I'm just going to walk through this. 206 00:11:29,814 --> 00:11:33,047 So, when you have two lines lining up 207 00:11:33,047 --> 00:11:36,300 that means they're going to run sequentially. 208 00:11:36,300 --> 00:11:39,590 If you see an if, or later here, we'll see a for. 209 00:11:39,590 --> 00:11:43,230 We haven't talked about for yet, but it's, it's like if. 210 00:11:43,230 --> 00:11:45,830 So, the fact that we go from this second line 211 00:11:45,830 --> 00:11:48,360 to this third line and move the indent in, we're actually 212 00:11:48,360 --> 00:11:51,160 creating a block that has to do with this if, 213 00:11:51,160 --> 00:11:53,350 and it, you can also kind of tell these, the if and 214 00:11:53,350 --> 00:11:56,000 the for end in a colon character. 215 00:11:56,000 --> 00:11:59,200 Now, we could pull this print back out, but we want 216 00:11:59,200 --> 00:12:02,470 it to be part of the if, so we maintain the indent. 217 00:12:02,470 --> 00:12:05,260 And then we're done with the if by pulling out. 218 00:12:05,260 --> 00:12:09,790 So we line the p with the i, and that means this is outside of the if. 219 00:12:11,110 --> 00:12:14,750 This for, which we haven't learned about for yet, for is another 220 00:12:14,750 --> 00:12:19,010 statement that ends in a colon, and afterwards you have to indent. 221 00:12:19,010 --> 00:12:22,230 Then you maintain the indent. Here's an if. 222 00:12:22,230 --> 00:12:25,080 But now we have an if, and we're already in, 223 00:12:25,080 --> 00:12:27,260 but that ends in a colon, so we go in farther. 224 00:12:28,580 --> 00:12:29,940 And now this is the block. 225 00:12:29,940 --> 00:12:35,660 Now, we come back out, and we line up with that if, right there, okay? 226 00:12:35,660 --> 00:12:39,220 And now, at the end of this, this indent, this x here 227 00:12:39,220 --> 00:12:42,610 comes all the way back out, so it lines up. 228 00:12:42,610 --> 00:12:44,820 The rest of these are kind of weird in that 229 00:12:44,820 --> 00:12:48,340 comments don't matter, blank lines don't matter. 230 00:12:48,340 --> 00:12:50,730 And so, it just is, sort of, you have to 231 00:12:50,730 --> 00:12:54,670 get, mentally get used to the notion that these don't count. 232 00:12:54,670 --> 00:12:56,686 They can really cognitively mess you up. 233 00:12:56,686 --> 00:12:59,590 So these don't count. 234 00:12:59,590 --> 00:13:01,310 And now, if I look through it, without, 235 00:13:01,310 --> 00:13:04,190 with the comments hidden, it starts in column one. 236 00:13:05,420 --> 00:13:09,530 Ignore, ignore, goes in, stays in, ignore, ignore, 237 00:13:09,530 --> 00:13:14,230 ignore, comes out. So that's, it all makes sense. 238 00:13:14,230 --> 00:13:17,720 Those comments and blank lines are just, kind of, confusion. 239 00:13:19,370 --> 00:13:23,970 So, increasing and decreasing indent has meaning in Python. 240 00:13:23,970 --> 00:13:26,380 We'll learn more about this in a bit. 241 00:13:26,380 --> 00:13:29,430 Our programs won't get this complex right away, but it's important to 242 00:13:29,430 --> 00:13:32,420 think, these indents aren't just pretty; 243 00:13:32,420 --> 00:13:35,220 they actually are communicating something to Python. 244 00:13:35,220 --> 00:13:38,239 And what they're communicating is, basically, what's in a block. 245 00:13:39,470 --> 00:13:41,210 And it shouldn't take you very long, when you 246 00:13:41,210 --> 00:13:45,340 start looking at Python, to sort of visualize these blocks. 247 00:13:45,340 --> 00:13:47,710 So, here, here's a big block. 248 00:13:47,710 --> 00:13:50,670 This block here, that's got these three things. 249 00:13:50,670 --> 00:13:52,460 And then, this is a block as well, and 250 00:13:52,460 --> 00:13:54,740 you can kind of say, well, here's an if statement. 251 00:13:54,740 --> 00:13:57,410 And then these are the two statements that are part of that if statement. 252 00:13:57,410 --> 00:14:00,930 So mentally, you kind of make these block pictures. 253 00:14:00,930 --> 00:14:03,700 So here's another block. This is that for loop. 254 00:14:03,700 --> 00:14:06,330 This part's the indented part, but then there's a block inside of the block. 255 00:14:06,330 --> 00:14:08,550 So you gotta kind of start seeing that as well. 256 00:14:08,550 --> 00:14:11,100 So this is a block that has to do with this green block 257 00:14:11,100 --> 00:14:14,550 is the, the one that has to do with the if. 258 00:14:15,580 --> 00:14:17,700 And then there's a block here, and then this is 259 00:14:17,700 --> 00:14:20,397 a great big block because this is where it finally de-indents. 260 00:14:20,397 --> 00:14:23,133 So, don't worry about it yet, but at some 261 00:14:23,133 --> 00:14:26,781 point you're just going to start seeing this indenting and de-indenting 262 00:14:26,781 --> 00:14:31,273 as defining blocks of code for various purposes. 263 00:14:31,273 --> 00:14:34,610 Now we don't have all the purposes yet, but we'll get there. 264 00:14:34,610 --> 00:14:39,900 So, we saw in that previous thing one block within a block. 265 00:14:39,900 --> 00:14:41,580 And, and we're going to do that. 266 00:14:41,580 --> 00:14:44,390 We can have ifs, we can have loops that 267 00:14:44,390 --> 00:14:46,360 get indented, but then we can indent even more. 268 00:14:46,360 --> 00:14:48,940 We call that nested, where there's an indented 269 00:14:48,940 --> 00:14:51,210 area that's in an area that's already indented. 270 00:14:52,740 --> 00:14:54,520 So here's a nested decision. 271 00:14:54,520 --> 00:14:55,870 And it might be easier to start on 272 00:14:55,870 --> 00:14:58,880 this side, where I'm going to have a first choice. 273 00:14:58,880 --> 00:15:01,290 Is x greater than 1, yes or no, and if 274 00:15:01,290 --> 00:15:03,740 it's yes, I'll do some work, and then I'm going to 275 00:15:03,740 --> 00:15:06,720 ask another question, and if that's yes, then I'm going to 276 00:15:06,720 --> 00:15:08,490 do this, then I'm going to come all the way back in. 277 00:15:08,490 --> 00:15:12,858 And the way we encode this in Python is, x equals 42, 278 00:15:12,858 --> 00:15:18,463 if x is greater than 1, it's true, so we continue working in the indent. 279 00:15:18,463 --> 00:15:21,963 And now we say, oh, if x is less than 100, which is still true, 280 00:15:21,963 --> 00:15:26,100 so we go in farther, and we do this, and now we come out. 281 00:15:26,100 --> 00:15:28,530 We don't come out to here, we actually keep going 282 00:15:28,530 --> 00:15:32,020 all the way to here, so that ends both blocks. 283 00:15:32,020 --> 00:15:34,680 And so if you sort of think about this, 284 00:15:34,680 --> 00:15:37,310 again this is where I want you to start seeing 285 00:15:37,310 --> 00:15:40,310 what's in a block of code and what's not in 286 00:15:40,310 --> 00:15:43,520 a block of code, and how the indents sort of, 287 00:15:43,520 --> 00:15:45,950 like, put a boundary on the blocks of code. 288 00:15:45,950 --> 00:15:49,640 And so, the first thing you should see is, sort of, like, that 289 00:15:49,640 --> 00:15:54,020 purple part, the, the x less than 100, print, that's kind of a box. 290 00:15:54,020 --> 00:15:57,690 And you can see the box on the, on the sort of flow diagram as well. 291 00:15:57,690 --> 00:16:00,080 The boxes are there. The boxes on the flow 292 00:16:00,080 --> 00:16:03,410 diagram are places where there's one entrance and one exit. 293 00:16:08,720 --> 00:16:11,210 And then there's also, sort of, the larger box, right? 294 00:16:11,210 --> 00:16:14,000 There's this if box that includes the smaller box. 295 00:16:14,000 --> 00:16:16,640 So, the, there's this nesting, which is boxes 296 00:16:16,640 --> 00:16:20,510 within boxes, or indented areas within indented areas. 297 00:16:26,080 --> 00:16:28,495 Now that was a, what we call a one-way decision, where 298 00:16:28,495 --> 00:16:30,985 you're doing if, and this code either runs or it doesn't run. 299 00:16:30,985 --> 00:16:35,320 It is extremely common to want to basically say, 300 00:16:35,320 --> 00:16:37,860 look, I'm going to do one of two things. I'm going to 301 00:16:37,860 --> 00:16:40,460 ask a question, if the question is true, I'm going to do 302 00:16:40,460 --> 00:16:43,290 one thing. If the question's false, I'm going to do another thing. 303 00:16:43,290 --> 00:16:45,590 So that's what we have shown here. 304 00:16:45,590 --> 00:16:49,100 We say, is x equals 4, is x equal to question mark? 305 00:16:49,100 --> 00:16:51,040 If it's yes, we're going to go here. 306 00:16:51,040 --> 00:16:52,930 If it's no, we're going to go here. 307 00:16:52,930 --> 00:16:55,180 We,re going to execute one or the other, and then we're going to continue. 308 00:16:56,390 --> 00:16:58,150 So we're really at a fork in the road here, right? 309 00:16:58,150 --> 00:17:00,720 We're, we're at a fork in the road, going to make 310 00:17:00,720 --> 00:17:05,000 a choice, and one or the other, but never both, right? 311 00:17:05,000 --> 00:17:08,349 So, we're going to do one thing, or we're going to do another thing. 312 00:17:08,349 --> 00:17:10,150 We're going to do one of the two, and 313 00:17:10,150 --> 00:17:12,460 depending on what the question that we ask, 314 00:17:12,460 --> 00:17:15,430 the question that we ask is, which one that we're going to do. 315 00:17:17,930 --> 00:17:19,099 So here's a little bit of code. 316 00:17:21,569 --> 00:17:25,950 x equals 4, is x greater than 2, the answer is yes. 317 00:17:25,950 --> 00:17:29,040 Then we come out and hit this else and we automatically 318 00:17:29,040 --> 00:17:33,140 skip, right, because we're only going to do one of the two. 319 00:17:33,140 --> 00:17:37,350 And here's the picture, x equals 4. Is x equal to yes? 320 00:17:37,350 --> 00:17:39,330 Print, done. 321 00:17:39,330 --> 00:17:43,190 Which means we'll never do both this and that, never do both, both sides. 322 00:17:43,190 --> 00:17:46,250 We're going to do one or the other of the sides. 323 00:17:46,250 --> 00:17:48,720 And just sort of going with the box, 324 00:17:48,720 --> 00:17:50,610 that is our box, oops, go back, go back. 325 00:17:53,410 --> 00:17:55,210 This is our box, right? 326 00:17:55,210 --> 00:17:58,100 It's sort of the indent followed by the final indent. 327 00:17:58,100 --> 00:18:00,210 The else is really kind of part of it. 328 00:18:00,210 --> 00:18:03,670 And then we can draw the picture here. It has one entry and one exit. 329 00:18:06,230 --> 00:18:10,524 Okay. So we have one-way ifs, and we have 330 00:18:10,524 --> 00:18:15,610 two-way ifs, and now we have multi-way ifs, okay? 331 00:18:15,610 --> 00:18:22,800 So, here is a multi-way if, 332 00:18:22,800 --> 00:18:30,682 and it introduces a new reserved word, elif, 333 00:18:30,682 --> 00:18:33,270 which is a combination of else and if. 334 00:18:33,270 --> 00:18:37,950 And this one, probably, is just as easy to talk about the picture here. 335 00:18:37,950 --> 00:18:40,016 The first question is asked, there's still 336 00:18:40,016 --> 00:18:42,110 going to only be one, there's only going to 337 00:18:42,110 --> 00:18:45,990 be one, one and only one of these three choices are going to run. 338 00:18:45,990 --> 00:18:49,550 Once it's run one, then it's done, okay? 339 00:18:49,550 --> 00:18:53,680 So the, the way to think about this, if x is less than 2, we're going to 340 00:18:53,680 --> 00:18:56,130 run this one, and then we're going to kind of 341 00:18:56,130 --> 00:18:58,700 flush all the way out to the bottom. 342 00:18:58,700 --> 00:19:01,300 If x is not less than 2, and it's less than 10, 343 00:19:01,300 --> 00:19:03,812 we're going to run this one, then flush out the bottom. 344 00:19:03,812 --> 00:19:05,900 And if x is not less than 2, and x is not 345 00:19:05,900 --> 00:19:11,690 less than 10, we're going to run this one, and flush out the bottom. 346 00:19:11,690 --> 00:19:18,299 So, one of these three, one, two, three, one of those three is going to run. 347 00:19:18,299 --> 00:19:22,600 And it's going to run based on the questions that are being asked. 348 00:19:22,600 --> 00:19:23,942 The questions do get asked 349 00:19:23,942 --> 00:19:26,487 in an order, and the order does matter, okay? 350 00:19:26,487 --> 00:19:29,298 So that is a multi-way if. 351 00:19:29,298 --> 00:19:34,650 If, else if, else. So this is kind of like an otherwise. 352 00:19:34,650 --> 00:19:38,650 The else is like an otherwise, you know, one way or another, we're going to run 353 00:19:38,650 --> 00:19:43,110 something, and if none of these first two have run, we will run this one. 354 00:19:43,110 --> 00:19:48,290 We call it a multi-way if, okay? 355 00:19:48,290 --> 00:19:51,780 So, here's an example of our multi-way if. 356 00:19:51,780 --> 00:19:55,021 That, if we say x equals 0, x equals 0. 357 00:19:55,021 --> 00:19:55,840 Is it less than 2? 358 00:19:55,840 --> 00:19:56,890 Yes, it is. 359 00:19:56,890 --> 00:20:00,570 So we run Small, print Small, and then we flush out the bottom. 360 00:20:02,170 --> 00:20:06,930 If we switch, instead, x to 5, x is 5. 361 00:20:06,930 --> 00:20:09,336 Is it less than 2? No, it is not less than 2. 362 00:20:09,336 --> 00:20:10,173 Is it less than 10? 363 00:20:10,173 --> 00:20:14,016 Well, 5 is less than 10. So the answer is yes, so we print Medium, 364 00:20:14,016 --> 00:20:18,910 then we flush out the bottom. One and only one are going to execute. 365 00:20:20,130 --> 00:20:22,515 Now, in this case, we got x is 20. 366 00:20:22,515 --> 00:20:25,480 And so we come through here. Is it less than 2? 367 00:20:25,480 --> 00:20:26,660 No, it is not. 368 00:20:26,660 --> 00:20:28,620 Is it less than 10? No, it is not. 369 00:20:28,620 --> 00:20:30,910 So we're going to do this one, and then flush out the bottom. 370 00:20:30,910 --> 00:20:38,580 If we go here, it's false, false, go here, all else being equal, we run that one. 371 00:20:38,580 --> 00:20:40,020 So this one doesn't run 372 00:20:40,020 --> 00:20:43,500 and that one doesn't run, right? Because these are like gateways. 373 00:20:43,500 --> 00:20:45,630 If it were true, it would run it. 374 00:20:45,630 --> 00:20:47,910 But it's false, so we're going to skip it. 375 00:20:47,910 --> 00:20:50,590 This one, it's false, so we're going to skip it. 376 00:20:50,590 --> 00:20:53,520 But then we hit the else, that's like a catch-all. 377 00:20:53,520 --> 00:20:57,993 And then if none of these were true, then it will run the else. 378 00:20:57,993 --> 00:21:01,575 Any questions? 379 00:21:01,575 --> 00:21:03,543 Okay. Well, 380 00:21:03,543 --> 00:21:08,600 I'm going to ask you a question, in a second. 381 00:21:11,600 --> 00:21:14,950 Okay, so just a couple of things that probably you're wondering about. 382 00:21:16,050 --> 00:21:17,130 You don't actually need an else. 383 00:21:18,250 --> 00:21:23,630 You can have a multi-way, x equals 5, if x is less than 2, there's no else here. 384 00:21:23,630 --> 00:21:25,830 You'll notice that this print just comes back. 385 00:21:25,830 --> 00:21:29,690 And so this way, it could, if both of these are false, it could 386 00:21:29,690 --> 00:21:34,490 skip them both and just run right through here, and there's no else clause, okay? 387 00:21:34,490 --> 00:21:36,720 So, in this case, if this one's 388 00:21:36,720 --> 00:21:40,170 going to, the way this one's going to run is, x equals 5 389 00:21:40,170 --> 00:21:43,440 if x is less than 2, it's, it's not. 390 00:21:43,440 --> 00:21:44,285 And it skips to here. 391 00:21:44,285 --> 00:21:49,150 Else if x is less than 10, which it is, it will run that one and come here. 392 00:21:49,150 --> 00:21:55,090 But, for a different value of x, like 95, boop boop. 393 00:21:56,560 --> 00:22:01,780 If x was 95, or 59, this would be false. It would skip it. 394 00:22:01,780 --> 00:22:04,912 This would, elif, would still be false, and it would skip it, 395 00:22:04,912 --> 00:22:08,205 and the only thing it would print out would be, All done. Okay? 396 00:22:08,205 --> 00:22:12,870 Okay, you can also have many elifs. 397 00:22:14,290 --> 00:22:17,945 So, better change to green. 398 00:22:17,945 --> 00:22:19,828 It checks this one, if it's true, it runs the first one. 399 00:22:19,828 --> 00:22:21,113 If it's false, it checks this one. 400 00:22:21,113 --> 00:22:25,240 If that's true, it run this one, and then it skips, right? 401 00:22:25,240 --> 00:22:26,830 And so, so the way to think about 402 00:22:26,830 --> 00:22:30,020 this is, is, it just goes through and checks this one 403 00:22:30,020 --> 00:22:33,310 false, this one false, false, false, oh, I finally found one. 404 00:22:33,310 --> 00:22:35,200 And now I'm done. 405 00:22:35,200 --> 00:22:39,490 It still is going to do one and only one of these. 406 00:22:39,490 --> 00:22:43,164 This one has an else, so that sooner or later, it is going to do one. 407 00:22:43,164 --> 00:22:49,703 And it only will do the else if all of these are false. All have to be false. 408 00:22:49,703 --> 00:22:52,500 Then it will, actually, come and hit the else clause. 409 00:22:53,558 --> 00:22:55,410 It's great, because there are lots of situations where 410 00:22:55,410 --> 00:22:58,340 you're like, oh, is it before eight in the morning? 411 00:22:58,340 --> 00:23:00,170 Or is it between eight and noon? 412 00:23:00,170 --> 00:23:02,070 Or is it between noon and five? 413 00:23:02,070 --> 00:23:05,780 Or after five? After midnight? I don't know. 414 00:23:05,780 --> 00:23:10,710 Okay, so, here, coming up, is a question. 415 00:23:12,370 --> 00:23:14,610 And, you, there's two puzzles and I'm going to 416 00:23:14,610 --> 00:23:16,459 stop so you can look at them for a while. 417 00:23:16,459 --> 00:23:18,811 And I want you to figure out, 418 00:23:18,811 --> 00:23:22,143 in both sides of this, which of the lines 419 00:23:22,143 --> 00:23:26,730 will not execute, regardless of the value for x. 420 00:23:26,730 --> 00:23:28,820 So in both sides, there is a line 421 00:23:28,820 --> 00:23:31,990 that won't execute, regardless of the value for x. 422 00:23:31,990 --> 00:23:33,180 Which will never print? 423 00:23:33,180 --> 00:23:38,210 There's two problems, problem A and problem B. 424 00:23:38,210 --> 00:23:42,276 Okay, I'll have some coffee while you think. 425 00:23:42,276 --> 00:23:44,276 [NOISE]. 426 00:23:54,859 --> 00:23:58,970 Okay, hopefully you paused it so that you could actually think for a bit. 427 00:23:58,970 --> 00:24:01,480 So, so I'm going to guess you probably 428 00:24:01,480 --> 00:24:03,440 got the first one right, that's pretty straightforward. 429 00:24:03,440 --> 00:24:06,462 I mean, actually, you're in great shape if you got both of them right. 430 00:24:06,462 --> 00:24:08,518 If you got any of them right, you're in great shape 431 00:24:08,518 --> 00:24:10,314 because that means you're starting to get it. 432 00:24:10,314 --> 00:24:15,157 Starting to like, oh, I'm seeing, kind of, this flow picture, there's a picture. 433 00:24:15,157 --> 00:24:17,044 I look at these characters that seemingly 434 00:24:17,044 --> 00:24:19,630 look like gibberish, and a picture arrives. 435 00:24:19,630 --> 00:24:23,710 Or a pattern of access execution arises. 436 00:24:23,710 --> 00:24:25,250 That's what we want to see. 437 00:24:25,250 --> 00:24:29,280 So, the in the first one, which will never print? 438 00:24:29,280 --> 00:24:32,890 Well, we're looking for kind of a value for x which will be defective. 439 00:24:32,890 --> 00:24:35,260 So if x is less than 2, we're going to do this. 440 00:24:35,260 --> 00:24:38,970 Else, if x is greater than or equal to 2, we'll do this, else we'll do that. 441 00:24:38,970 --> 00:24:40,650 Well, here's the problem with this one. 442 00:24:40,650 --> 00:24:44,150 For all values of x, it is, is either going, 443 00:24:44,150 --> 00:24:48,670 x is less than 2 is either going to be true or greater than equal to 2. 444 00:24:48,670 --> 00:24:51,560 Greater than or equal to be, pah, for X to be 445 00:24:51,560 --> 00:24:54,210 greater than or equal to 2 is going to be true. 446 00:24:54,210 --> 00:24:56,510 So it's going to run this one, or it's going to run that one. 447 00:24:56,510 --> 00:24:57,970 So for big numbers, numbers above 2, 448 00:24:57,970 --> 00:25:01,650 it's going to run this one; below 2, it's going to run that one. 449 00:25:01,650 --> 00:25:05,060 So this one is never going to run, okay? 450 00:25:05,060 --> 00:25:07,170 Because the, one of the first two is going to be 451 00:25:07,170 --> 00:25:11,320 true, and so the third else situation is not going to run. 452 00:25:11,320 --> 00:25:12,120 Hope you got that right. 453 00:25:13,550 --> 00:25:16,540 Okay, so let's take a look at the next one, okay? 454 00:25:16,540 --> 00:25:19,560 So the question is, you know, if x is less than 2, do this, 455 00:25:19,560 --> 00:25:23,100 if x is less 20, do that, and if x is less than 10, do this, 456 00:25:23,100 --> 00:25:28,574 and otherwise do that. Well, the one that will never execute 457 00:25:28,574 --> 00:25:34,009 is this one. And, x equals 15, 458 00:25:34,009 --> 00:25:38,977 no, x equals 15 is a bad one, x equals 5 is the one that will, 459 00:25:38,977 --> 00:25:43,966 sort of, cause it to behave badly. 460 00:25:43,966 --> 00:25:48,640 And so, if x is 5, this is false. 461 00:25:48,640 --> 00:25:53,316 If x is less than 20, this is true, and then it's done. 462 00:25:53,316 --> 00:25:58,648 So the problem is, this is the one that will never execute, because 463 00:25:58,648 --> 00:26:04,046 if a value is less than 10, it's also less than 20, so this will be true. 464 00:26:04,046 --> 00:26:06,626 So for a value like 5, which happens to be less than 10, 465 00:26:06,626 --> 00:26:10,410 which you would think would cause that line to execute, does not. 466 00:26:11,540 --> 00:26:14,160 This one executes because it's checked first. 467 00:26:14,160 --> 00:26:17,350 Now, if we just moved this code, took this code and 468 00:26:17,350 --> 00:26:21,050 we moved it down here, then it would make more sense, okay? 469 00:26:21,050 --> 00:26:21,780 Oops. 470 00:26:21,780 --> 00:26:23,960 If we moved it down there, it would make more sense. 471 00:26:23,960 --> 00:26:29,950 But basically, the answer to these is, change color, this one won't 472 00:26:29,950 --> 00:26:35,910 execute, and this one will never execute for any value, so there's the answer. 473 00:26:38,440 --> 00:26:39,910 Okay, so we're almost done with conditionals. 474 00:26:39,910 --> 00:26:43,740 I want to show you one more kind of conditional. 475 00:26:43,740 --> 00:26:44,780 It's a little bit different. 476 00:26:46,110 --> 00:26:51,308 It's not a bit of code structure that you make, it is, 477 00:26:51,308 --> 00:26:56,621 it is dealing with the fact that some things may blow up. 478 00:26:56,621 --> 00:27:00,395 Like, if you read a number from a user and you try to convert it 479 00:27:00,395 --> 00:27:02,763 to a floating point number, as you may 480 00:27:02,763 --> 00:27:05,797 have already done in some of your homework, 481 00:27:05,797 --> 00:27:06,764 it can blow up. 482 00:27:06,764 --> 00:27:12,060 You know it's going to blow up, but you don't exactly want it to kill your program. 483 00:27:12,060 --> 00:27:17,430 So, the concept of try and except are, hey, this is a dangerous thing. 484 00:27:17,430 --> 00:27:18,430 I know it might blow up. 485 00:27:18,430 --> 00:27:20,531 I know exactly what it might blow up, but I don't want to die 486 00:27:20,531 --> 00:27:22,990 I don't want to stop my program when it blows up. 487 00:27:22,990 --> 00:27:24,260 I want to continue. 488 00:27:24,260 --> 00:27:26,690 And that's the purpose of the except block. 489 00:27:26,690 --> 00:27:28,520 So, here's a little bit of code. 490 00:27:28,520 --> 00:27:31,210 And, you know, it's, we've done this code before. 491 00:27:31,210 --> 00:27:33,880 This is code that's kind of similar to, like 492 00:27:33,880 --> 00:27:36,850 your rate and pay homework, where you read a string 493 00:27:36,850 --> 00:27:40,710 using raw input, you converted it using float, but 494 00:27:40,710 --> 00:27:43,950 then if you typed in Fred, the thing blows up. 495 00:27:43,950 --> 00:27:46,190 So we're kind of simulating that right here. 496 00:27:46,190 --> 00:27:49,080 So here we have a variable astr called Hello Bob, 497 00:27:49,080 --> 00:27:51,580 and then we try to turn it into an integer. 498 00:27:51,580 --> 00:27:53,370 And then we're going to print that out, and then we have 499 00:27:53,370 --> 00:27:56,920 another string called one, and that has the letters 1, 2, 3. 500 00:27:56,920 --> 00:28:00,580 We convert that to an integer, and then we print that one out. 501 00:28:00,580 --> 00:28:03,600 The problem is, is that when this runs, 502 00:28:05,700 --> 00:28:07,020 this is going to fail. 503 00:28:07,020 --> 00:28:10,450 It's going to fail with this traceback, okay? 504 00:28:10,450 --> 00:28:17,240 And the problem is, is when the traceback happens, the program stops executing. 505 00:28:17,240 --> 00:28:22,330 The traceback is Python's way of asking you, hey, this 506 00:28:22,330 --> 00:28:25,330 would be bad, I don't know what to do, I'm stopping. 507 00:28:25,330 --> 00:28:30,478 So that means that the rest of your program is gone, right? 508 00:28:30,478 --> 00:28:30,682 It, 509 00:28:30,682 --> 00:28:33,700 The fact that we had stuff down here doesn't matter. 510 00:28:33,700 --> 00:28:36,700 This line died with the traceback. 511 00:28:36,700 --> 00:28:37,790 It stopped. 512 00:28:37,790 --> 00:28:40,550 It doesn't, like, give you a traceback and then keep going. 513 00:28:40,550 --> 00:28:43,661 It gives you a traceback, and that's the end. 514 00:28:43,661 --> 00:28:45,293 Now this might be something, instead of 515 00:28:45,293 --> 00:28:48,060 just the string, Hello Bob, which is insane. 516 00:28:48,060 --> 00:28:51,530 Data might have come from a raw input, where the user was typing, and you say, 517 00:28:51,530 --> 00:28:53,450 give me a number, and they type something 518 00:28:53,450 --> 00:28:55,650 that's not a number, and this would blow up. 519 00:28:55,650 --> 00:28:57,210 It's like, hey, I know it's going to blow up. 520 00:28:58,270 --> 00:29:03,613 The problem with this is that you don't, oops, erp, clear the thing. 521 00:29:03,613 --> 00:29:06,403 Oh and now we have to start it on fire again. 522 00:29:06,403 --> 00:29:07,964 Okay, it's on fire. 523 00:29:07,964 --> 00:29:11,550 The problem is, is that in a sense, this program is you. 524 00:29:12,570 --> 00:29:16,970 If you recall, we have you as the, typing these commands 525 00:29:16,970 --> 00:29:21,010 into these scripts, feeding the central processing unit, answering the question 526 00:29:21,010 --> 00:29:22,430 what next? 527 00:29:22,430 --> 00:29:28,320 So you should take it a little personally when your program gets a traceback. 528 00:29:28,320 --> 00:29:32,180 because that means you, in the form of your program, have been vaporized. 529 00:29:32,180 --> 00:29:35,870 And you're not present to give any more instructions. 530 00:29:35,870 --> 00:29:36,620 It stops. 531 00:29:36,620 --> 00:29:40,450 It stops dead in its tracks. You are gone. 532 00:29:40,450 --> 00:29:44,350 So, we want to make sure we control this behavior. 533 00:29:44,350 --> 00:29:46,790 We know it might blow up, 534 00:29:46,790 --> 00:29:52,600 and we want to capture the situation where it does, and execute alternate code. 535 00:29:52,600 --> 00:29:53,580 Okay. 536 00:29:53,580 --> 00:29:55,120 So here it goes. 537 00:29:55,120 --> 00:29:56,780 It's a bit of syntax. 538 00:29:56,780 --> 00:30:00,230 I mentioned that it uses the try and except keywords. 539 00:30:00,230 --> 00:30:04,560 These are reserved words in Python. And then it's a little indented block. 540 00:30:04,560 --> 00:30:08,390 So, astr equals Hello Bob, great. 541 00:30:08,390 --> 00:30:12,010 Try means, we're about to do something dangerous, let's take out some 542 00:30:12,010 --> 00:30:13,490 insurance policy on it. 543 00:30:13,490 --> 00:30:16,420 And that is, we're going to convert this to an integer. 544 00:30:16,420 --> 00:30:19,830 Take astr, convert it to an integer, put it in istr. 545 00:30:20,860 --> 00:30:24,430 If that works, great, we'll just continue on, and ignore this except. 546 00:30:24,430 --> 00:30:27,415 If it blows up, we're going to jump into the 547 00:30:27,415 --> 00:30:31,220 except block, and then we'll have alternate substitute code. 548 00:30:31,220 --> 00:30:35,330 In this case, I'm going to set the variable to negative 1 as an indicator. 549 00:30:35,330 --> 00:30:37,800 Then I'll print it out, and I'll do it again. 550 00:30:37,800 --> 00:30:40,860 Try this code, and away we go. 551 00:30:40,860 --> 00:30:45,578 So, when this runs, we know exactly how it's going to run. 552 00:30:45,578 --> 00:30:51,516 It, whoa, woop, come back. 553 00:30:51,516 --> 00:30:55,284 We'll set this string, the try takes out the insurance, 554 00:30:55,284 --> 00:31:01,846 this blows up, so it runs down to here and runs this part, and then it'll 555 00:31:01,846 --> 00:31:07,300 print First minus 1. And it sets the string to 1, 2, 3, not 556 00:31:07,300 --> 00:31:11,788 123, but 1, 2, 3 as a string. It takes out an insurance policy. 557 00:31:11,788 --> 00:31:17,835 This time it works, and that puts istr is going to be 123, 558 00:31:17,835 --> 00:31:22,191 so we don't run the accept code, and so out comes the 559 00:31:22,191 --> 00:31:27,969 second 1, 2, 3, okay? So the try is, take out insurance on this 560 00:31:27,969 --> 00:31:31,097 little bit of code, and if it fails, 561 00:31:31,097 --> 00:31:35,563 run this alternate code. If not, skip the alternate code. 562 00:31:35,563 --> 00:31:37,079 So it's kind of conditional. 563 00:31:37,079 --> 00:31:40,499 If you put multiple lines in the block between 564 00:31:40,499 --> 00:31:44,530 the try and the except, it runs until one dies. 565 00:31:44,530 --> 00:31:46,080 So it doesn't come back, okay? 566 00:31:46,080 --> 00:31:50,430 It's not taking insurance out on, separately, on all three statements. 567 00:31:50,430 --> 00:31:52,690 It's like, here's a block of stuff, and if anything blows up, 568 00:31:52,690 --> 00:31:56,870 stop. And the things that run do run. 569 00:31:56,870 --> 00:31:59,078 So if, this is really kind of bad code, 570 00:31:59,078 --> 00:32:02,010 because you really don't want the print in here. 571 00:32:02,010 --> 00:32:05,350 It's actually a good idea on the try except to have as little in the 572 00:32:05,350 --> 00:32:09,300 try block as you possibly can, so you're real clear on what's going to fail. 573 00:32:11,580 --> 00:32:15,530 but, so here we come in, shh, it's Bob, so it's going to fail. 574 00:32:15,530 --> 00:32:16,400 We run this. 575 00:32:16,400 --> 00:32:18,170 That runs successfully. 576 00:32:18,170 --> 00:32:23,540 This blows up, so it quits and jumps into the except blocks and continues. 577 00:32:23,540 --> 00:32:28,231 The point is, is that this code never executes, never executes. 578 00:32:28,231 --> 00:32:31,380 The other point is, this code does execute. 579 00:32:31,380 --> 00:32:34,094 Just because this blew up, this is already executed, 580 00:32:34,094 --> 00:32:36,808 it might have done something other, more complex than 581 00:32:36,808 --> 00:32:40,280 print Hello, okay? So, so there you go. 582 00:32:40,280 --> 00:32:42,410 So, if we look at this kind of in a picture, 583 00:32:42,410 --> 00:32:45,494 we, we set this through the try block, it runs, it runs. 584 00:32:45,494 --> 00:32:51,479 And the, the try except kind of has this escape hatch that says, if there is 585 00:32:51,479 --> 00:32:55,089 a [SOUND] explosion somehow, then it runs this 586 00:32:55,089 --> 00:32:59,622 alternate code and then it comes out and finishes, okay? 587 00:32:59,622 --> 00:33:01,872 And, again, this, it doesn't go 588 00:33:01,872 --> 00:33:06,767 back and finish the block, and it doesn't undo the work that is done by that. 589 00:33:06,767 --> 00:33:09,170 So it doesn't un-execute it. 590 00:33:09,170 --> 00:33:11,850 If it executes and works, it just keeps on going, then 591 00:33:11,850 --> 00:33:16,790 it blows up, and then sort of flushes its way out, okay? 592 00:33:16,790 --> 00:33:21,130 So here's an example of how you might do this in a running program, like the 593 00:33:21,130 --> 00:33:22,980 programs that you're about to be assigned, where 594 00:33:22,980 --> 00:33:25,710 you're supposed to check for user input having errors. 595 00:33:26,920 --> 00:33:33,020 So, here is a little conversion of a number, and 596 00:33:33,020 --> 00:33:38,430 and so we're saying, you know, enter a number, and we're putting a string into rawstr. 597 00:33:38,430 --> 00:33:42,260 It's a string, and and so, we don't know. 598 00:33:42,260 --> 00:33:44,460 And here's where we're going to convert it into an integer, 599 00:33:44,460 --> 00:33:46,348 and we're just not sure if it's going to work or not. 600 00:33:46,348 --> 00:33:51,758 So, we know how int works. It either converts it or it blows up. 601 00:33:51,758 --> 00:33:51,950 So we know 602 00:33:51,950 --> 00:33:53,486 it's going to do that, we just don't 603 00:33:53,486 --> 00:33:55,627 know what the user's going to type, we don't know. 604 00:33:55,627 --> 00:33:58,439 So we have to take out insurance on it. So this runs, 605 00:33:58,439 --> 00:34:00,029 and then we do a try, and then we try to convert it, 606 00:34:00,029 --> 00:34:04,137 and if it works, it's great, and if 607 00:34:04,137 --> 00:34:07,370 it fails, it runs this and sets it to negative 1. 608 00:34:07,370 --> 00:34:11,860 And afterwards, we either have the number or negative 1. 609 00:34:11,860 --> 00:34:16,199 And so, if the person enters 42, it says Nice work. 610 00:34:17,199 --> 00:34:19,440 Well, let's show you where it runs. 611 00:34:19,440 --> 00:34:24,664 If the person says 42, it runs through here, gets the string 42, converts that 612 00:34:24,664 --> 00:34:29,650 to an integer, skips here, and then says, Nice work, and that's how it runs. 613 00:34:29,650 --> 00:34:33,614 If, on the other hand, they type forty two, the words, 614 00:34:33,614 --> 00:34:36,820 this gets to be the string forty two. 615 00:34:36,820 --> 00:34:39,940 It runs here, this blows up. 616 00:34:39,940 --> 00:34:42,290 So it comes and runs this part here. 617 00:34:42,290 --> 00:34:46,520 And then it says, if ival is greater than 0, which is not true, 618 00:34:46,520 --> 00:34:50,510 so it runs this part and says Not a number. 619 00:34:50,510 --> 00:34:54,380 So this is our way of compensating for user input 620 00:34:54,380 --> 00:34:59,300 that might have errors in it, errors that we're anticipating, right? 621 00:34:59,300 --> 00:35:02,210 You'd, you'd rather at least put up some kind of a 622 00:35:02,210 --> 00:35:05,060 message, rather than just have a traceback, 623 00:35:05,060 --> 00:35:06,800 if you're writing code for somebody else. 624 00:35:06,800 --> 00:35:08,080 It just kind of is 625 00:35:08,080 --> 00:35:12,719 not very classy. So, 626 00:35:12,719 --> 00:35:20,490 the classic program to do this is a time-and-a-half for overtime pay. 627 00:35:23,240 --> 00:35:26,376 So you get some pay rate like $10 an hour for your first 40 hours, 628 00:35:26,376 --> 00:35:29,951 and then you get 15 hours, for any hours above it. 629 00:35:29,951 --> 00:35:34,297 So you have to sort of say, oh, okay, if this ends up being, this ends up being 630 00:35:34,297 --> 00:35:40,273 some kind of a thing, where, let me draw that picture a little better. 631 00:35:40,273 --> 00:35:43,654 Hours greater than 40, you're going to do one thing, and 632 00:35:43,654 --> 00:35:47,570 if hours are less than 40, you're going to do another thing. 633 00:35:47,570 --> 00:35:49,800 So you have two payout calculations. 634 00:35:49,800 --> 00:35:53,725 If the hours are greater than 40, then you're going to do 635 00:35:53,725 --> 00:35:58,900 a overtime calculation, which is kind of like, 40 times the regular rate. 636 00:35:58,900 --> 00:36:01,910 And then, the number of excess hours, like 5 overtime hours 637 00:36:01,910 --> 00:36:05,620 times the reg, rate times one-and-a-half. 638 00:36:05,620 --> 00:36:10,560 So this is kind of the calculation that happens if the hours are greater than 40. 639 00:36:10,560 --> 00:36:15,960 And then, if the hours are less than 40, it's just pay 640 00:36:15,960 --> 00:36:18,690 equals rate times hours. 641 00:36:18,690 --> 00:36:23,660 So it, you're going to do one of two calculations, depending on how it works. 642 00:36:23,660 --> 00:36:27,720 So that's one of the programming problems for this chapter. 643 00:36:27,720 --> 00:36:29,330 That's a classic. 644 00:36:29,330 --> 00:36:31,240 It's the classic if, then, else. 645 00:36:31,240 --> 00:36:33,080 You can actually do it with an if, then if you're tricky. 646 00:36:33,080 --> 00:36:36,462 There's a lot of ways to do this, so pick a, pick one and do it. 647 00:36:36,462 --> 00:36:41,247 Now the next thing I want you to do is, I want you to take that 648 00:36:41,247 --> 00:36:47,685 same program, do it again in another, another assignment, or another problem in 649 00:36:47,685 --> 00:36:54,430 the chapter, and that is have some kind of a non-numeric input, and have it blow up. 650 00:36:54,430 --> 00:37:00,060 So if they type, you know, something like nine, put out an error. 651 00:37:00,060 --> 00:37:02,510 Or, if they type something here, put out an error. 652 00:37:02,510 --> 00:37:06,070 Now, don't write a loop. No loop. 653 00:37:07,110 --> 00:37:11,240 This is one execution of the program, and this is another execution of the program. 654 00:37:11,240 --> 00:37:12,160 Later, we can write loops. 655 00:37:12,160 --> 00:37:15,240 We haven't even talked about loops. So this is running it twice. 656 00:37:15,240 --> 00:37:17,750 All I want you to do is exit. 657 00:37:17,750 --> 00:37:20,290 So take a look in the book as to how to just get out. 658 00:37:20,290 --> 00:37:23,080 So it, it's, it, I don't want you to try to say, I'm going to 659 00:37:23,080 --> 00:37:26,900 prompt for these numbers until I get a good one, we'll do that later. 660 00:37:26,900 --> 00:37:28,910 I just want you to deal with the fact that 661 00:37:28,910 --> 00:37:32,390 you read a thing, you use the try 662 00:37:32,390 --> 00:37:34,900 to convert it to a float and see if it works. 663 00:37:34,900 --> 00:37:36,160 And if you don't, you just quit. 664 00:37:36,160 --> 00:37:40,500 Don't, don't, don't try to be tricky and repeatedly prompt. 665 00:37:40,500 --> 00:37:46,554 So don't repeatedly prompt. One prompt, and then 666 00:37:46,554 --> 00:37:51,870 quit, okay? So, this is contintous-, 667 00:37:51,870 --> 00:37:57,400 conditional execution, if, if then else, and then I added a little bit with 668 00:37:57,400 --> 00:37:59,810 the try and except, as well. 669 00:37:59,810 --> 00:38:03,869 And the try and except is really a limited kind of a problem. 670 00:38:03,869 --> 00:38:09,113 It really is to compensate for errors that you anticipate are going to happen, and 671 00:38:09,113 --> 00:38:11,317 you can imagine what you want to do 672 00:38:11,317 --> 00:38:14,650 as a replacement for what those errors are, okay? 673 00:38:14,650 --> 00:38:15,330 See you next lecture.