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