1 00:00:00,400 --> 00:00:01,460 So in the previous video, 2 00:00:01,470 --> 00:00:04,840 we briefly discussed the concepts of algorithms as 3 00:00:04,849 --> 00:00:07,900 well as the if else else if statements, 4 00:00:08,850 --> 00:00:13,619 now we're gonna put it into action. We're actually going to write a program 5 00:00:14,060 --> 00:00:18,709 that will request a number between 1 to 9 from the user. 6 00:00:19,260 --> 00:00:22,950 Now, if the number is greater than four, we are going to say 7 00:00:23,079 --> 00:00:24,190 why so much? 8 00:00:26,950 --> 00:00:31,229 Meaning that if it is four or less, 9 00:00:31,479 --> 00:00:35,040 we're going to say why? So little. 10 00:00:35,369 --> 00:00:37,819 Now the very last line I have blurred it for 11 00:00:37,830 --> 00:00:40,909 a particular reason because once we write this first program, 12 00:00:40,919 --> 00:00:41,599 we're going to 13 00:00:41,919 --> 00:00:43,639 add an extra condition. 14 00:00:43,650 --> 00:00:47,200 I don't want to show you what that extra condition is just yet. 15 00:00:47,549 --> 00:00:48,080 So 16 00:00:48,189 --> 00:00:51,240 let's focus on writing this program. All right. So let me just drag this away. 17 00:00:51,680 --> 00:00:54,939 All right. And let me just remove this. 18 00:00:55,090 --> 00:00:55,750 OK. 19 00:00:56,189 --> 00:01:01,900 So the first part of the program is we want to request a number between 1 to 9. So 20 00:01:02,240 --> 00:01:04,819 what can we do here? 21 00:01:05,190 --> 00:01:06,940 Well, first of all, 22 00:01:07,430 --> 00:01:11,620 we can create a variable called N, 23 00:01:11,819 --> 00:01:13,459 all right, for the number 24 00:01:14,569 --> 00:01:18,319 and we're going to pass, remember we worked with the input function. 25 00:01:18,809 --> 00:01:21,519 So I'm gonna say input because that's the 26 00:01:21,720 --> 00:01:24,779 function that would request an input from the user. 27 00:01:25,139 --> 00:01:28,309 So I'm gonna say input and I'm going to say, 28 00:01:29,519 --> 00:01:33,489 enter a number between 1 29 00:01:33,849 --> 00:01:35,099 to 9. 30 00:01:35,889 --> 00:01:36,430 OK. 31 00:01:38,180 --> 00:01:41,970 Let's just, first of all, make sure that this is working. OK. 32 00:01:42,050 --> 00:01:43,650 Enter number between 1 to 9. 33 00:01:44,430 --> 00:01:45,940 Let me just create some space 34 00:01:46,360 --> 00:01:47,849 between the one and nine 35 00:01:48,269 --> 00:01:49,980 run again. OK. 36 00:01:50,360 --> 00:01:51,050 All right. 37 00:01:51,190 --> 00:01:51,830 So the 38 00:01:52,059 --> 00:01:53,599 first line works. 39 00:01:53,830 --> 00:01:54,800 Now 40 00:01:55,389 --> 00:01:58,290 we want to check if the number 41 00:01:58,709 --> 00:02:00,080 is between 42 00:02:01,120 --> 00:02:03,779 or is less than a four, right? So 43 00:02:04,250 --> 00:02:06,160 I'm going to say if 44 00:02:07,190 --> 00:02:07,809 all right, 45 00:02:07,940 --> 00:02:08,610 and then 46 00:02:09,610 --> 00:02:12,869 the integer because we want to convert 47 00:02:13,350 --> 00:02:16,750 whatever number we provided, we wanna convert it to an integer because 48 00:02:17,210 --> 00:02:18,669 the user could be very naughty. 49 00:02:18,679 --> 00:02:24,070 They could decide to provide, say 1.14 0.5 you know, 7.6 something like that. So 50 00:02:24,339 --> 00:02:29,789 we want to keep everything as an integer. So I'm gonna wrap our variable right now 51 00:02:30,130 --> 00:02:31,529 in the integer function, 52 00:02:31,660 --> 00:02:32,389 right? So 53 00:02:33,130 --> 00:02:36,110 I'm going to say if integer N, 54 00:02:36,679 --> 00:02:40,050 OK? And now space and then the symbol for greater than 55 00:02:40,410 --> 00:02:41,990 greater than four. 56 00:02:43,100 --> 00:02:48,080 OK? And now pay very close attention. I'm going to add the double colon. OK? 57 00:02:48,250 --> 00:02:49,770 You need to add this 58 00:02:49,899 --> 00:02:53,029 whenever you commit your el statements. OK? So 59 00:02:53,289 --> 00:02:54,559 I'm going to say that. 60 00:02:55,630 --> 00:02:56,449 And now 61 00:02:58,039 --> 00:03:02,350 by default, actually, Python has written out this line for me, 62 00:03:02,589 --> 00:03:04,410 but I'm going to say 63 00:03:05,059 --> 00:03:07,899 why so much. 64 00:03:08,679 --> 00:03:09,300 OK? 65 00:03:10,360 --> 00:03:11,610 Now enter 66 00:03:13,070 --> 00:03:14,000 and now 67 00:03:14,970 --> 00:03:15,970 I'm going to say 68 00:03:17,119 --> 00:03:18,210 else 69 00:03:18,729 --> 00:03:19,550 if 70 00:03:19,880 --> 00:03:20,889 however, 71 00:03:31,309 --> 00:03:31,960 OK. 72 00:03:32,460 --> 00:03:34,190 And now I'm going to say 73 00:03:34,559 --> 00:03:35,970 else 74 00:03:38,029 --> 00:03:39,660 and then new line 75 00:03:40,619 --> 00:03:42,050 indent print 76 00:03:43,100 --> 00:03:44,520 and then I'm going to say, 77 00:03:45,839 --> 00:03:47,740 uh why 78 00:03:47,970 --> 00:03:48,960 so 79 00:03:49,169 --> 00:03:50,330 little? 80 00:03:52,050 --> 00:03:52,710 Ok. 81 00:03:54,119 --> 00:03:55,820 So I'm gonna go ahead right now. 82 00:03:56,740 --> 00:04:01,839 Oh, by the way, make sure in your LS you also have the double colon as well. OK. 83 00:04:03,119 --> 00:04:04,410 Let's go ahead right now 84 00:04:04,580 --> 00:04:05,779 and run the program 85 00:04:06,779 --> 00:04:08,059 and let's see. So 86 00:04:08,279 --> 00:04:12,160 enter number between 1 to 9, I'm going to say six 87 00:04:12,360 --> 00:04:13,449 press enter, 88 00:04:13,809 --> 00:04:17,399 you can see right there. It actually works. Why so much? 89 00:04:17,890 --> 00:04:22,000 Let's run the program again. I'm going to say three, 90 00:04:22,970 --> 00:04:24,700 three right there. 91 00:04:25,350 --> 00:04:27,910 And then it says why so little? So 92 00:04:28,079 --> 00:04:30,390 it actually works as you can see. 93 00:04:30,549 --> 00:04:34,019 Now, I'm gonna go over this once again. OK? So first of all, 94 00:04:34,450 --> 00:04:40,019 we have and which is going to be the variable that will accept the input from the user. 95 00:04:40,559 --> 00:04:44,420 And the input is a question, enter a number between 1 to 9. OK. 96 00:04:44,670 --> 00:04:46,549 So now we're gonna create a condition to that 97 00:04:46,559 --> 00:04:49,299 will check to see the value of that number. 98 00:04:49,309 --> 00:04:49,739 So 99 00:04:50,170 --> 00:04:51,660 we have decided 100 00:04:51,809 --> 00:04:54,269 to convert the input to an integer. 101 00:04:54,279 --> 00:04:59,100 That's why we have the I NTA a question you might have here is wait a minute, 102 00:04:59,109 --> 00:05:00,750 Alex Previously, when we had 103 00:05:01,600 --> 00:05:05,209 a function inside of another function, we have to use the brackets, right? 104 00:05:05,540 --> 00:05:10,070 The if else this is not exactly a function. 105 00:05:10,480 --> 00:05:10,809 OK? 106 00:05:10,820 --> 00:05:12,230 It's more of an algorithm, 107 00:05:12,239 --> 00:05:15,570 more of kind of like a loop just checking to see the value of something. 108 00:05:15,579 --> 00:05:16,190 It's not 109 00:05:16,390 --> 00:05:18,170 an actual function itself. That's why 110 00:05:18,399 --> 00:05:22,470 we don't have the integer wrapped in brackets. OK? So 111 00:05:22,779 --> 00:05:27,549 it's gonna check if the integer and if it's greater than four will print, 112 00:05:27,559 --> 00:05:29,579 why so much else? 113 00:05:29,589 --> 00:05:31,149 Meaning that if the number 114 00:05:31,559 --> 00:05:39,019 is four or less than four, print, why so little you can see it works, right? And 115 00:05:39,170 --> 00:05:44,630 the only thing you need to pay attention to would be the double colon after 116 00:05:45,190 --> 00:05:48,070 the four and also after the LS. 117 00:05:48,630 --> 00:05:51,980 And then it's also very good practice that whenever you're working with, 118 00:05:51,989 --> 00:05:56,059 if L statements or else if you have good indentation, 119 00:05:56,390 --> 00:06:00,769 I didn't do this. OK? I didn't have my print statements 120 00:06:01,109 --> 00:06:04,829 directly underneath the E and L statements. This is, this is not good at all. 121 00:06:04,839 --> 00:06:06,109 This is very, very poor 122 00:06:06,279 --> 00:06:07,869 programming syntax. So 123 00:06:07,980 --> 00:06:10,970 you always want to have your indentation 124 00:06:11,100 --> 00:06:12,250 correctly. OK? 125 00:06:12,649 --> 00:06:14,559 Have that whenever you're working with the eve 126 00:06:14,709 --> 00:06:15,600 statements. 127 00:06:16,260 --> 00:06:17,149 OK? 128 00:06:17,299 --> 00:06:18,299 Now 129 00:06:18,760 --> 00:06:20,470 I'm gonna bring back 130 00:06:21,269 --> 00:06:22,380 our program 131 00:06:23,260 --> 00:06:27,140 and look at that. I have added an extra condition saying 132 00:06:27,609 --> 00:06:32,059 that what if the number is a specific number which is five? 133 00:06:33,019 --> 00:06:33,679 Well, 134 00:06:34,170 --> 00:06:35,040 we want to say 135 00:06:35,929 --> 00:06:37,079 bingo. 136 00:06:37,549 --> 00:06:38,230 OK. 137 00:06:39,250 --> 00:06:44,109 So how exactly are we going to do this? OK. 138 00:06:44,790 --> 00:06:46,850 Let me track this away. 139 00:06:48,209 --> 00:06:48,859 OK. 140 00:06:49,529 --> 00:06:51,029 Now check this out. 141 00:06:51,809 --> 00:06:55,269 I'm gonna come over here where it says print, why so much? 142 00:06:55,390 --> 00:06:58,170 And I'm going to press enter because we want to add 143 00:06:58,179 --> 00:07:01,820 the else if in between the open if statement and the 144 00:07:01,950 --> 00:07:03,250 closing L statement. 145 00:07:03,369 --> 00:07:03,829 So 146 00:07:04,519 --> 00:07:05,760 now I'm going to say 147 00:07:07,170 --> 00:07:07,779 L 148 00:07:09,390 --> 00:07:10,049 if 149 00:07:11,459 --> 00:07:14,029 this is the short form of writing, else, if so 150 00:07:14,339 --> 00:07:15,739 else if we use in 151 00:07:16,619 --> 00:07:17,429 Python, 152 00:07:18,040 --> 00:07:20,760 so if the integer 153 00:07:21,390 --> 00:07:22,260 and 154 00:07:24,209 --> 00:07:26,299 is equal or 155 00:07:26,559 --> 00:07:27,709 the integer N, 156 00:07:28,529 --> 00:07:29,989 if it's equal 157 00:07:31,040 --> 00:07:32,380 to five, 158 00:07:32,579 --> 00:07:37,059 the equal is going to be double equal sign, not just one. OK? 159 00:07:37,269 --> 00:07:37,739 So 160 00:07:38,119 --> 00:07:42,179 else if, if the internet provided by the user is five 161 00:07:42,470 --> 00:07:44,480 and now I'm going to add the double colon 162 00:07:45,260 --> 00:07:46,459 and then I'm going to say 163 00:07:48,459 --> 00:07:51,239 sorry, I'm gonna say print 164 00:07:52,279 --> 00:07:53,459 then in brackets 165 00:07:55,709 --> 00:07:56,570 bingo. 166 00:07:58,250 --> 00:07:59,040 OK. 167 00:07:59,369 --> 00:08:00,869 So I'm gonna go ahead right now. 168 00:08:01,299 --> 00:08:02,510 We're on the program. 169 00:08:03,769 --> 00:08:07,980 And now let's check to see if this actually works. 170 00:08:08,459 --> 00:08:11,850 First of all, I'm going to type in three. OK. 171 00:08:12,209 --> 00:08:14,579 OK. So it's still working. Why so little? 172 00:08:14,799 --> 00:08:15,269 OK? 173 00:08:16,339 --> 00:08:18,589 Uh Let's try eight 174 00:08:19,670 --> 00:08:21,239 by so much. OK? 175 00:08:21,679 --> 00:08:26,529 And now if I say five and I press enter, 176 00:08:27,279 --> 00:08:28,269 guess what? 177 00:08:28,470 --> 00:08:30,309 It's not saying Bingo, 178 00:08:30,429 --> 00:08:33,989 it's still saying why so much? 179 00:08:34,808 --> 00:08:35,750 You wanna know why? 180 00:08:36,539 --> 00:08:38,679 Because the Python program 181 00:08:39,090 --> 00:08:46,919 executes line by line. Now look at the hierarchy in here. The very first condition 182 00:08:47,049 --> 00:08:52,119 that Python is checking for is to see if the number is greater than four 183 00:08:52,450 --> 00:08:55,280 and because five is greater than four, 184 00:08:55,609 --> 00:08:59,919 it immediately says, OK, why so much? It doesn't even 185 00:09:00,059 --> 00:09:02,479 go further down 186 00:09:02,650 --> 00:09:05,479 to check if the number is equal to five. 187 00:09:06,369 --> 00:09:09,200 Because the very first statement is basically saying, hey, 188 00:09:09,210 --> 00:09:14,429 if the number is greater than four, then print, why so much. 189 00:09:15,330 --> 00:09:17,299 So to prove this to you, 190 00:09:17,520 --> 00:09:21,070 what I'm gonna do here is this OK? 191 00:09:21,950 --> 00:09:22,859 I'm going to say 192 00:09:23,690 --> 00:09:24,390 that 193 00:09:24,590 --> 00:09:28,469 let's actually change the hierarchy in here. So 194 00:09:29,030 --> 00:09:30,739 we will check first of all, 195 00:09:31,210 --> 00:09:35,520 if the integer is actually equal to five. 196 00:09:35,900 --> 00:09:39,169 OK? Let that be the very first thing it checks. 197 00:09:39,599 --> 00:09:41,270 And now over here, 198 00:09:41,679 --> 00:09:44,659 we will check to see if the integer 199 00:09:45,000 --> 00:09:46,669 is greater 200 00:09:47,210 --> 00:09:48,390 than four. 201 00:09:49,080 --> 00:09:52,059 So I've basically just changed the hierarchy. 202 00:09:52,070 --> 00:09:53,700 So the very first thing I was gonna check right 203 00:09:53,710 --> 00:09:57,169 now is if the integer is actually equal to five, 204 00:09:57,390 --> 00:09:59,250 it's going to say uh bingo. 205 00:10:00,010 --> 00:10:02,409 Else if, if it's 206 00:10:02,640 --> 00:10:04,599 not equal to five 207 00:10:05,289 --> 00:10:06,919 and it's greater than four, 208 00:10:07,070 --> 00:10:09,429 we're going to say why so much. 209 00:10:09,549 --> 00:10:11,510 And then else if it's 210 00:10:11,690 --> 00:10:17,789 not equal to five and if it's not greater than four, then print why so little? 211 00:10:18,770 --> 00:10:19,309 OK, 212 00:10:19,989 --> 00:10:23,289 let's run the program. So I'm going to come over here right now. Run, 213 00:10:24,950 --> 00:10:29,250 let me first of all, add five now and see. Enter 214 00:10:29,500 --> 00:10:32,700 and there you go. Bingo. It actually worked. 215 00:10:32,840 --> 00:10:35,690 Let's run the program again. What if I typed in to 216 00:10:35,979 --> 00:10:42,909 enter? Why? So little? Once again, let's go with seven. Enter. Why? So much? 217 00:10:42,919 --> 00:10:44,070 So you can see right now 218 00:10:44,190 --> 00:10:45,700 it works. 219 00:10:46,049 --> 00:10:51,250 So the hierarchy of your commands is extremely important 220 00:10:51,520 --> 00:10:55,940 whenever you're working with Python or programming in general, 221 00:10:56,119 --> 00:10:59,780 when you're checking for conditions, always keep in mind 222 00:11:00,099 --> 00:11:01,659 that once Python 223 00:11:02,270 --> 00:11:05,460 has gotten a condition that satisfied, 224 00:11:05,599 --> 00:11:11,059 it will immediately print out the result, it's not going to go any further down 225 00:11:11,260 --> 00:11:15,159 the remaining lines in your program. So 226 00:11:15,270 --> 00:11:17,280 the hierarchy of your conditions, 227 00:11:17,349 --> 00:11:21,299 the conditions of that check your commands is so so important. 228 00:11:21,400 --> 00:11:24,179 And then of course, also this little 229 00:11:24,289 --> 00:11:27,109 knowing where to add the 230 00:11:27,429 --> 00:11:30,969 coons, things like that. This is something you will eventually get used to. 231 00:11:31,200 --> 00:11:33,330 It might be a bit overwhelming in the very beginning. 232 00:11:33,340 --> 00:11:35,239 But as you begin to write more programs, 233 00:11:35,380 --> 00:11:37,739 you will eventually grow accustomed 234 00:11:37,950 --> 00:11:38,960 to them. 235 00:11:39,080 --> 00:11:43,380 So that's it for working with the if else, if statements, thank you for watching, 236 00:11:43,390 --> 00:11:45,059 I will see you in the next class.