1 00:00:00,000 --> 00:00:04,650 So, we saw in that previous thing one block within a block. 2 00:00:04,650 --> 00:00:06,330 And, and we're going to do that. 3 00:00:06,330 --> 00:00:09,140 We can have ifs, we can have loops that 4 00:00:09,140 --> 00:00:11,110 get indented, but then we can indent even more. 5 00:00:11,110 --> 00:00:13,690 We call that nested, where there's an indented 6 00:00:13,690 --> 00:00:15,960 area that's in an area that's already indented. 7 00:00:17,490 --> 00:00:19,270 So here's a nested decision. 8 00:00:19,270 --> 00:00:20,620 And it might be easier to start on 9 00:00:20,620 --> 00:00:23,630 this side, where I'm going to have a first choice. 10 00:00:23,630 --> 00:00:26,040 Is x greater than 1, yes or no, and if 11 00:00:26,040 --> 00:00:28,490 it's yes, I'll do some work, and then I'm going to 12 00:00:28,490 --> 00:00:31,470 ask another question, and if that's yes, then I'm going to 13 00:00:31,470 --> 00:00:33,240 do this, then I'm going to come all the way back in. 14 00:00:33,240 --> 00:00:37,608 And the way we encode this in Python is, x equals 42, 15 00:00:37,608 --> 00:00:43,213 if x is greater than 1, it's true, so we continue working in the indent. 16 00:00:43,213 --> 00:00:46,713 And now we say, oh, if x is less than 100, which is still true, 17 00:00:46,713 --> 00:00:50,850 so we go in farther, and we do this, and now we come out. 18 00:00:50,850 --> 00:00:53,280 We don't come out to here, we actually keep going 19 00:00:53,280 --> 00:00:56,770 all the way to here, so that ends both blocks. 20 00:00:56,770 --> 00:00:59,430 And so if you sort of think about this, 21 00:00:59,430 --> 00:01:02,060 again this is where I want you to start seeing 22 00:01:02,060 --> 00:01:05,060 what's in a block of code and what's not in 23 00:01:05,060 --> 00:01:08,270 a block of code, and how the indents sort of, 24 00:01:08,270 --> 00:01:10,700 like, put a boundary on the blocks of code. 25 00:01:10,700 --> 00:01:14,390 And so, the first thing you should see is, sort of, like, that 26 00:01:14,390 --> 00:01:18,770 purple part, the, the x less than 100, print, that's kind of a box. 27 00:01:18,770 --> 00:01:22,440 And you can see the box on the, on the sort of flow diagram as well. 28 00:01:22,440 --> 00:01:24,830 The boxes are there. The boxes on the flow 29 00:01:24,830 --> 00:01:28,160 diagram are places where there's one entrance and one exit. 30 00:01:33,470 --> 00:01:35,960 And then there's also, sort of, the larger box, right? 31 00:01:35,960 --> 00:01:38,750 There's this if box that includes the smaller box. 32 00:01:38,750 --> 00:01:41,390 So, the, there's this nesting, which is boxes 33 00:01:41,390 --> 00:01:45,260 within boxes, or indented areas within indented areas. 34 00:01:50,830 --> 00:01:53,245 Now that was a, what we call a one-way decision, where 35 00:01:53,245 --> 00:01:55,735 you're doing if, and this code either runs or it doesn't run. 36 00:01:55,735 --> 00:02:00,070 It is extremely common to want to basically say, 37 00:02:00,070 --> 00:02:02,610 look, I'm going to do one of two things. I'm going to 38 00:02:02,610 --> 00:02:05,210 ask a question, if the question is true, I'm going to do 39 00:02:05,210 --> 00:02:08,040 one thing. If the question's false, I'm going to do another thing. 40 00:02:08,040 --> 00:02:10,340 So that's what we have shown here. 41 00:02:10,340 --> 00:02:13,850 We say, is x equals 4, is x equal to question mark? 42 00:02:13,850 --> 00:02:15,790 If it's yes, we're going to go here. 43 00:02:15,790 --> 00:02:17,680 If it's no, we're going to go here. 44 00:02:17,680 --> 00:02:19,930 We,re going to execute one or the other, and then we're going to continue. 45 00:02:21,140 --> 00:02:22,900 So we're really at a fork in the road here, right? 46 00:02:22,900 --> 00:02:25,470 We're, we're at a fork in the road, going to make 47 00:02:25,470 --> 00:02:29,750 a choice, and one or the other, but never both, right? 48 00:02:29,750 --> 00:02:33,100 So, we're going to do one thing, or we're going to do another thing. 49 00:02:33,100 --> 00:02:34,900 We're going to do one of the two, and 50 00:02:34,900 --> 00:02:37,210 depending on what the question that we ask, 51 00:02:37,210 --> 00:02:40,180 the question that we ask is, which one that we're going to do. 52 00:02:42,680 --> 00:02:43,850 So here's a little bit of code. 53 00:02:46,320 --> 00:02:50,700 x equals 4, is x greater than 2, the answer is yes. 54 00:02:50,700 --> 00:02:53,790 Then we come out and hit this else and we automatically 55 00:02:53,790 --> 00:02:57,890 skip, right, because we're only going to do one of the two. 56 00:02:57,890 --> 00:03:02,100 And here's the picture, x equals 4. Is x equal to yes? 57 00:03:02,100 --> 00:03:04,080 Print, done. 58 00:03:04,080 --> 00:03:07,940 Which means we'll never do both this and that, never do both, both sides. 59 00:03:07,940 --> 00:03:11,000 We're going to do one or the other of the sides. 60 00:03:11,000 --> 00:03:13,470 And just sort of going with the box, 61 00:03:13,470 --> 00:03:15,360 that is our box, oops, go back, go back. 62 00:03:18,160 --> 00:03:19,960 This is our box, right? 63 00:03:19,960 --> 00:03:22,850 It's sort of the indent followed by the final indent. 64 00:03:22,850 --> 00:03:24,960 The else is really kind of part of it. 65 00:03:24,960 --> 00:03:28,420 And then we can draw the picture here. It has one entry and one exit. 66 00:03:30,980 --> 00:03:35,274 Okay. So we have one-way ifs, and we have 67 00:03:35,274 --> 00:03:40,360 two-way ifs, and now we have multi-way ifs, okay? 68 00:03:40,360 --> 00:03:47,550 So, here is a multi-way if, 69 00:03:47,550 --> 00:03:55,432 and it introduces a new reserved word, elif, 70 00:03:55,432 --> 00:03:58,020 which is a combination of else and if. 71 00:03:58,020 --> 00:04:02,700 And this one, probably, is just as easy to talk about the picture here. 72 00:04:02,700 --> 00:04:04,766 The first question is asked, there's still 73 00:04:04,766 --> 00:04:06,860 going to only be one, there's only going to 74 00:04:06,860 --> 00:04:10,740 be one, one and only one of these three choices are going to run. 75 00:04:10,740 --> 00:04:14,300 Once it's run one, then it's done, okay? 76 00:04:14,300 --> 00:04:18,430 So the, the way to think about this, if x is less than 2, we're going to 77 00:04:18,430 --> 00:04:20,880 run this one, and then we're going to kind of 78 00:04:20,880 --> 00:04:23,450 flush all the way out to the bottom. 79 00:04:23,450 --> 00:04:26,050 If x is not less than 2, and it's less than 10, 80 00:04:26,050 --> 00:04:28,562 we're going to run this one, then flush out the bottom. 81 00:04:28,562 --> 00:04:30,650 And if x is not less than 2, and x is not 82 00:04:30,650 --> 00:04:36,440 less than 10, we're going to run this one, and flush out the bottom. 83 00:04:36,440 --> 00:04:43,049 So, one of these three, one, two, three, one of those three is going to run. 84 00:04:43,049 --> 00:04:47,350 And it's going to run based on the questions that are being asked. 85 00:04:47,350 --> 00:04:48,692 The questions do get asked 86 00:04:48,692 --> 00:04:51,237 in an order, and the order does matter, okay? 87 00:04:51,237 --> 00:04:54,048 So that is a multi-way if. 88 00:04:54,048 --> 00:04:59,400 If, else if, else. So this is kind of like an otherwise. 89 00:04:59,400 --> 00:05:03,400 The else is like an otherwise, you know, one way or another, we're going to run 90 00:05:03,400 --> 00:05:07,860 something, and if none of these first two have run, we will run this one. 91 00:05:07,860 --> 00:05:13,040 We call it a multi-way if, okay? 92 00:05:13,040 --> 00:05:16,530 So, here's an example of our multi-way if. 93 00:05:16,530 --> 00:05:19,771 That, if we say x equals 0, x equals 0. 94 00:05:19,771 --> 00:05:20,590 Is it less than 2? 95 00:05:20,590 --> 00:05:21,640 Yes, it is. 96 00:05:21,640 --> 00:05:25,320 So we run Small, print Small, and then we flush out the bottom. 97 00:05:26,920 --> 00:05:31,680 If we switch, instead, x to 5, x is 5. 98 00:05:31,680 --> 00:05:34,086 Is it less than 2? No, it is not less than 2. 99 00:05:34,086 --> 00:05:34,923 Is it less than 10? 100 00:05:34,923 --> 00:05:38,766 Well, 5 is less than 10. So the answer is yes, so we print Medium, 101 00:05:38,766 --> 00:05:43,660 then we flush out the bottom. One and only one are going to execute. 102 00:05:44,880 --> 00:05:47,265 Now, in this case, we got x is 20. 103 00:05:47,265 --> 00:05:50,230 And so we come through here. Is it less than 2? 104 00:05:50,230 --> 00:05:51,410 No, it is not. 105 00:05:51,410 --> 00:05:53,370 Is it less than 10? No, it is not. 106 00:05:53,370 --> 00:05:55,660 So we're going to do this one, and then flush out the bottom. 107 00:05:55,660 --> 00:06:03,330 If we go here, it's false, false, go here, all else being equal, we run that one. 108 00:06:03,330 --> 00:06:04,770 So this one doesn't run 109 00:06:04,770 --> 00:06:08,250 and that one doesn't run, right? Because these are like gateways. 110 00:06:08,250 --> 00:06:10,380 If it were true, it would run it. 111 00:06:10,380 --> 00:06:12,660 But it's false, so we're going to skip it. 112 00:06:12,660 --> 00:06:15,340 This one, it's false, so we're going to skip it. 113 00:06:15,340 --> 00:06:18,270 But then we hit the else, that's like a catch-all. 114 00:06:18,270 --> 00:06:22,743 And then if none of these were true, then it will run the else. 115 00:06:22,743 --> 00:06:26,325 Any questions? 116 00:06:26,325 --> 00:06:28,293 Okay. Well, 117 00:06:28,293 --> 00:06:33,350 I'm going to ask you a question, in a second. 118 00:06:36,350 --> 00:06:39,700 Okay, so just a couple of things that probably you're wondering about. 119 00:06:40,800 --> 00:06:41,880 You don't actually need an else. 120 00:06:43,000 --> 00:06:48,380 You can have a multi-way, x equals 5, if x is less than 2, there's no else here. 121 00:06:48,380 --> 00:06:50,580 You'll notice that this print just comes back. 122 00:06:50,580 --> 00:06:54,440 And so this way, it could, if both of these are false, it could 123 00:06:54,440 --> 00:06:59,240 skip them both and just run right through here, and there's no else clause, okay? 124 00:06:59,240 --> 00:07:01,470 So, in this case, if this one's 125 00:07:01,470 --> 00:07:04,920 going to, the way this one's going to run is, x equals 5 126 00:07:04,920 --> 00:07:08,190 if x is less than 2, it's, it's not. 127 00:07:08,190 --> 00:07:09,035 And it skips to here. 128 00:07:09,035 --> 00:07:13,900 Else if x is less than 10, which it is, it will run that one and come here. 129 00:07:13,900 --> 00:07:19,840 But, for a different value of x, like 95, boop boop. 130 00:07:21,310 --> 00:07:26,530 If x was 95, or 59, this would be false. It would skip it. 131 00:07:26,530 --> 00:07:29,662 This would, elif, would still be false, and it would skip it, 132 00:07:29,662 --> 00:07:32,955 and the only thing it would print out would be, All done. Okay? 133 00:07:32,955 --> 00:07:37,620 Okay, you can also have many elifs. 134 00:07:39,040 --> 00:07:42,695 So, better change to green. 135 00:07:42,695 --> 00:07:44,578 It checks this one, if it's true, it runs the first one. 136 00:07:44,578 --> 00:07:45,863 If it's false, it checks this one. 137 00:07:45,863 --> 00:07:49,990 If that's true, it run this one, and then it skips, right? 138 00:07:49,990 --> 00:07:51,580 And so, so the way to think about 139 00:07:51,580 --> 00:07:54,770 this is, is, it just goes through and checks this one 140 00:07:54,770 --> 00:07:58,060 false, this one false, false, false, oh, I finally found one. 141 00:07:58,060 --> 00:07:59,950 And now I'm done. 142 00:07:59,950 --> 00:08:04,240 It still is going to do one and only one of these. 143 00:08:04,240 --> 00:08:07,914 This one has an else, so that sooner or later, it is going to do one. 144 00:08:07,914 --> 00:08:14,453 And it only will do the else if all of these are false. All have to be false. 145 00:08:14,453 --> 00:08:17,250 Then it will, actually, come and hit the else clause. 146 00:08:18,308 --> 00:08:20,160 It's great, because there are lots of situations where 147 00:08:20,160 --> 00:08:23,090 you're like, oh, is it before eight in the morning? 148 00:08:23,090 --> 00:08:24,920 Or is it between eight and noon? 149 00:08:24,920 --> 00:08:26,820 Or is it between noon and five? 150 00:08:26,820 --> 00:08:30,530 Or after five? After midnight? I don't know. 151 00:08:30,530 --> 00:08:35,460 Okay, so, here, coming up, is a question. 152 00:08:37,120 --> 00:08:39,360 And, you, there's two puzzles and I'm going to 153 00:08:39,360 --> 00:08:41,209 stop so you can look at them for a while. 154 00:08:41,209 --> 00:08:43,561 And I want you to figure out, 155 00:08:43,561 --> 00:08:46,893 in both sides of this, which of the lines 156 00:08:46,893 --> 00:08:51,480 will not execute, regardless of the value for x. 157 00:08:51,480 --> 00:08:53,570 So in both sides, there is a line 158 00:08:53,570 --> 00:08:56,740 that won't execute, regardless of the value for x. 159 00:08:56,740 --> 00:08:57,930 Which will never print? 160 00:08:57,930 --> 00:09:02,960 There's two problems, problem A and problem B. 161 00:09:02,960 --> 00:09:07,026 Okay, I'll have some coffee while you think. 162 00:09:07,026 --> 00:09:09,026 [NOISE]. 163 00:09:19,609 --> 00:09:23,720 Okay, hopefully you paused it so that you could actually think for a bit. 164 00:09:23,720 --> 00:09:26,230 So, so I'm going to guess you probably 165 00:09:26,230 --> 00:09:28,190 got the first one right, that's pretty straightforward. 166 00:09:28,190 --> 00:09:31,212 I mean, actually, you're in great shape if you got both of them right. 167 00:09:31,212 --> 00:09:33,268 If you got any of them right, you're in great shape 168 00:09:33,268 --> 00:09:35,064 because that means you're starting to get it. 169 00:09:35,064 --> 00:09:39,907 Starting to like, oh, I'm seeing, kind of, this flow picture, there's a picture. 170 00:09:39,907 --> 00:09:41,794 I look at these characters that seemingly 171 00:09:41,794 --> 00:09:44,380 look like gibberish, and a picture arrives. 172 00:09:44,380 --> 00:09:48,460 Or a pattern of access execution arises. 173 00:09:48,460 --> 00:09:50,000 That's what we want to see. 174 00:09:50,000 --> 00:09:54,030 So, the in the first one, which will never print? 175 00:09:54,030 --> 00:09:57,640 Well, we're looking for kind of a value for x which will be defective. 176 00:09:57,640 --> 00:10:00,010 So if x is less than 2, we're going to do this. 177 00:10:00,010 --> 00:10:03,720 Else, if x is greater than or equal to 2, we'll do this, else we'll do that. 178 00:10:03,720 --> 00:10:05,400 Well, here's the problem with this one. 179 00:10:05,400 --> 00:10:08,900 For all values of x, it is, is either going, 180 00:10:08,900 --> 00:10:13,420 x is less than 2 is either going to be true or greater than equal to 2. 181 00:10:13,420 --> 00:10:16,310 Greater than or equal to be, pah, for X to be 182 00:10:16,310 --> 00:10:18,960 greater than or equal to 2 is going to be true. 183 00:10:18,960 --> 00:10:21,260 So it's going to run this one, or it's going to run that one. 184 00:10:21,260 --> 00:10:22,720 So for big numbers, numbers above 2, 185 00:10:22,720 --> 00:10:26,400 it's going to run this one; below 2, it's going to run that one. 186 00:10:26,400 --> 00:10:29,810 So this one is never going to run, okay? 187 00:10:29,810 --> 00:10:31,920 Because the, one of the first two is going to be 188 00:10:31,920 --> 00:10:36,070 true, and so the third else situation is not going to run. 189 00:10:36,070 --> 00:10:36,870 Hope you got that right. 190 00:10:38,300 --> 00:10:41,290 Okay, so let's take a look at the next one, okay? 191 00:10:41,290 --> 00:10:44,310 So the question is, you know, if x is less than 2, do this, 192 00:10:44,310 --> 00:10:47,850 if x is less 20, do that, and if x is less than 10, do this, 193 00:10:47,850 --> 00:10:53,324 and otherwise do that. Well, the one that will never execute 194 00:10:53,324 --> 00:10:58,759 is this one. And, x equals 15, 195 00:10:58,759 --> 00:11:03,727 no, x equals 15 is a bad one, x equals 5 is the one that will, 196 00:11:03,727 --> 00:11:08,716 sort of, cause it to behave badly. 197 00:11:08,716 --> 00:11:13,390 And so, if x is 5, this is false. 198 00:11:13,390 --> 00:11:18,066 If x is less than 20, this is true, and then it's done. 199 00:11:18,066 --> 00:11:23,398 So the problem is, this is the one that will never execute, because 200 00:11:23,398 --> 00:11:28,796 if a value is less than 10, it's also less than 20, so this will be true. 201 00:11:28,796 --> 00:11:31,376 So for a value like 5, which happens to be less than 10, 202 00:11:31,376 --> 00:11:35,160 which you would think would cause that line to execute, does not. 203 00:11:36,290 --> 00:11:38,910 This one executes because it's checked first. 204 00:11:38,910 --> 00:11:42,100 Now, if we just moved this code, took this code and 205 00:11:42,100 --> 00:11:45,800 we moved it down here, then it would make more sense, okay? 206 00:11:45,800 --> 00:11:46,530 Oops. 207 00:11:46,530 --> 00:11:48,710 If we moved it down there, it would make more sense. 208 00:11:48,710 --> 00:11:54,700 But basically, the answer to these is, change color, this one won't 209 00:11:54,700 --> 00:12:00,660 execute, and this one will never execute for any value, so there's the answer. 210 00:12:03,190 --> 00:12:04,660 Okay, so we're almost done with conditionals.