WEBVTT 1 00:00:02.210 --> 00:00:05.570 So, in the previous video, we started talking a little bit about program flow. 2 00:00:05.570 --> 00:00:09.490 So, let's now continue on with that, and actually talk about the if statement. 3 00:00:09.490 --> 00:00:12.720 So, what I've done is, I've created a new project in IntelliJ. 4 00:00:12.720 --> 00:00:14.660 And I've called it ifprogramflow. 5 00:00:14.660 --> 00:00:19.540 And I'm going to right-click that now, click on New, select Python File, and 6 00:00:19.540 --> 00:00:21.360 I'm just gonna call it ifprogramflow. 7 00:00:23.310 --> 00:00:25.280 Making sure that I'm actually clicked in there. 8 00:00:25.280 --> 00:00:29.120 Okay, so we've seen how we can store values in variables, 9 00:00:29.120 --> 00:00:30.740 and we can then, print them out. 10 00:00:30.740 --> 00:00:33.540 And we've also looked at how to get input from the keyboard. 11 00:00:33.540 --> 00:00:36.290 So, we now start getting Python to make decisions 12 00:00:36.290 --> 00:00:40.160 based on the values of variables, and the input received from the keyboard. 13 00:00:40.160 --> 00:00:42.330 As a quick recap, just from the following program, 14 00:00:42.330 --> 00:00:44.250 just to see where we were at in the previous video. 15 00:00:44.250 --> 00:00:46.251 So, we can type name Equals input. 16 00:00:49.831 --> 00:00:51.390 Please enter your name. 17 00:00:54.090 --> 00:00:55.071 Now, we can type age equals input. 18 00:00:57.791 --> 00:01:03.183 How old are you, placement field, and 19 00:01:03.183 --> 00:01:10.790 we're going to put format name, you print age. 20 00:01:10.790 --> 00:01:13.360 So, notice how we can apply formatting to any string, 21 00:01:13.360 --> 00:01:17.070 even the ones that we're going to be using as a prompt to the input function. 22 00:01:17.070 --> 00:01:21.090 So, in the first line we've created a variable called name. 23 00:01:21.090 --> 00:01:24.870 And we're going to accept the name that's been entered by the user 24 00:01:24.870 --> 00:01:28.640 from the keyboard, and put that into the variable called name. 25 00:01:28.640 --> 00:01:32.510 Then, the next line, line three, we've got an age, we'll ask it to, 26 00:01:32.510 --> 00:01:36.100 for the user to put how old they are, and we're using a replacement field, 27 00:01:36.100 --> 00:01:39.410 to actually format, and print the name that was entered on line two, and 28 00:01:39.410 --> 00:01:44.490 share that on the string, on the screen, along with the prompt to enter their age, 29 00:01:44.490 --> 00:01:47.010 and on the fourth line, line four, we actually printed out. 30 00:01:47.010 --> 00:01:50.490 So, if we run this, please enter your name. 31 00:01:50.490 --> 00:01:53.080 Tim. How old are you Tim? 32 00:01:53.080 --> 00:01:56.660 I'll say I'm 21, I certainly wish that I was Press Enter > 21. 33 00:01:56.660 --> 00:02:00.020 So, we can say that's a bit of a recap we're at. 34 00:02:00.020 --> 00:02:03.280 Now, the input function returns a string data type. 35 00:02:03.280 --> 00:02:05.420 So, in order to actually convert that 21. 36 00:02:05.420 --> 00:02:08.770 That number that I actually typed into a number. 37 00:02:08.770 --> 00:02:11.500 If we want age to represent a number, rather than a string, 38 00:02:11.500 --> 00:02:14.040 we need to actually convert it. 39 00:02:14.040 --> 00:02:16.310 We do that using the int function. 40 00:02:16.310 --> 00:02:19.370 Before making some decisions based on the person's age. 41 00:02:19.370 --> 00:02:23.040 So, what we can do is actually change this, to put it int, 42 00:02:23.040 --> 00:02:26.750 which again converts, what's been typed into a stream. 43 00:02:26.750 --> 00:02:27.350 Then, we actually 44 00:02:27.350 --> 00:02:29.550 have to add a bracket right on the end of the line to make that valid. 45 00:02:29.550 --> 00:02:34.070 When we go to run it, we will see that there aren't any differences, so 46 00:02:34.070 --> 00:02:35.340 it will do that same thing. 47 00:02:35.340 --> 00:02:41.320 The only thing is we've changed it now, so that the data type, because we've selected 48 00:02:41.320 --> 00:02:45.560 the int, it's going to be a number Instead of what it was before which was a string. 49 00:02:45.560 --> 00:02:48.330 So now, that we've done that what we can do is we can actually get 50 00:02:48.330 --> 00:02:52.480 the computer to actually try, and do some processing based on that age. 51 00:02:52.480 --> 00:02:58.920 So, lets do that, so I'm gonna put if, if age is greater than or equal to, 52 00:02:58.920 --> 00:03:03.120 that's what those two symbols are that means it greater than or equal to 18. 53 00:03:03.120 --> 00:03:05.900 Then we put a colon and we press Enter to go to the next line. 54 00:03:05.900 --> 00:03:10.260 Then we're going to print ("You are old enough to vote."). 55 00:03:10.260 --> 00:03:12.921 So, if we run that. 56 00:03:15.621 --> 00:03:18.173 Before we do that, let's actually type in something that's not a number, and 57 00:03:18.173 --> 00:03:19.580 see what happens. 58 00:03:19.580 --> 00:03:21.110 We're gonna type in the words twenty one. 59 00:03:21.110 --> 00:03:23.100 We actually got an error. 60 00:03:24.250 --> 00:03:27.480 And the error is that it actually, basically just is expecting a number, but 61 00:03:27.480 --> 00:03:30.180 it actually receives some text, and it's got a little bit confused. 62 00:03:30.180 --> 00:03:34.540 So, keep that in mind, that if you specify something like we've done here, and 63 00:03:34.540 --> 00:03:36.980 an input, it's gonna assume that that is the case. 64 00:03:36.980 --> 00:03:39.660 But don't worry too much about that, because we're gonna be covering 65 00:03:39.660 --> 00:03:42.320 error handling, and input validation in the course. 66 00:03:42.320 --> 00:03:43.820 But for now, to be valid, 67 00:03:43.820 --> 00:03:46.140 we need to make sure that we're only entering valid numbers. 68 00:03:46.140 --> 00:03:47.228 So, we're gonna go back, and do it again. 69 00:03:47.228 --> 00:03:47.858 Tim? 70 00:03:47.858 --> 00:03:52.730 Twenty-one, and you can see we've got the message printed "You are old enough 71 00:03:52.730 --> 00:03:56.250 to vote." And, once we've got the age in the numeric format, 72 00:03:56.250 --> 00:03:59.540 we can actually use the greater than, and the equal, as we've used here, 73 00:03:59.540 --> 00:04:02.610 to actually check to see whether it is greater or equal than 18. 74 00:04:02.610 --> 00:04:06.090 And we're doing that using an if statement, with a condition to check. 75 00:04:06.090 --> 00:04:10.220 So, if the condition is true, in this case the age was greater than, or equal to 18, 76 00:04:10.220 --> 00:04:13.080 then, we're printing the message that you're old enough to vote. 77 00:04:13.080 --> 00:04:16.430 If the age is less than 18, nothing happens. 78 00:04:16.430 --> 00:04:18.050 It actually would go into the next line. 79 00:04:18.050 --> 00:04:20.980 Now, that's a little but unfriendly, now what we can do we can code you for 80 00:04:20.980 --> 00:04:24.070 that we can put in something called an else. 81 00:04:24.070 --> 00:04:28.626 Press back, press space will delete press else or top else. 82 00:04:28.626 --> 00:04:32.081 . And press Enter. 83 00:04:32.081 --> 00:04:33.410 Print. 84 00:04:33.410 --> 00:04:38.910 Please come back in, and we're gonna put our replacement field. 85 00:04:38.910 --> 00:04:41.770 Let's use .format. 86 00:04:41.770 --> 00:04:46.020 Let's actually get the computer to decide, how many years I've gotta come back with. 87 00:04:46.020 --> 00:04:46.600 And we can do that. 88 00:04:46.600 --> 00:04:49.390 We can actually put the number of years which was 18, 89 00:04:49.390 --> 00:04:53.050 less the current age that was typed in, so 18 take whatever. 90 00:04:53.050 --> 00:04:56.940 So, if I've actually typed in the age of 15, it would be 18 take 15 is 3. 91 00:04:56.940 --> 00:05:00.370 So, if we actually run that now, firstly, 92 00:05:00.370 --> 00:05:04.600 I'm putting my name, I'll put 21 we still get the same message, and notice how this 93 00:05:04.600 --> 00:05:09.360 line here is basically ignored, and that's because the first part was correct. 94 00:05:09.360 --> 00:05:13.270 So, how the program works is, it comes to, and looks for this condition, and 95 00:05:13.270 --> 00:05:17.490 if that is true, in other words, in this case what was typed in is greater than or 96 00:05:17.490 --> 00:05:20.670 equal to 18, then that line is executed. 97 00:05:20.670 --> 00:05:24.750 And then, that line is executed, not the next line with the else isn't it, and 98 00:05:24.750 --> 00:05:30.000 then, the actual code jumps past the else, and the print and goes on to next part. 99 00:05:30.000 --> 00:05:34.670 But otherwise, so if the age entered isn't greater than a towards at 17 or 100 00:05:34.670 --> 00:05:36.690 less, we get the second part. 101 00:05:36.690 --> 00:05:38.280 And just to confirm that, we can run that again. 102 00:05:39.890 --> 00:05:43.630 Tim, if I put in 15, please come back in three. 103 00:05:43.630 --> 00:05:47.300 And I should go back to change that, and put that in years, like so. 104 00:05:47.300 --> 00:05:51.160 If we actually run that again, Tim 15. 105 00:05:51.160 --> 00:05:54.360 Please come back in three years. 106 00:05:54.360 --> 00:05:58.510 Now, the other thing to note here is the block of code 107 00:05:58.510 --> 00:06:02.830 that's within these conditions can have more than one line of code to execute. 108 00:06:02.830 --> 00:06:07.880 So, in other words we can come back, and put, you're old enough to vote, print, 109 00:06:09.080 --> 00:06:17.660 please put an X in the ballot box or more correctly print x in the box. 110 00:06:17.660 --> 00:06:19.300 Else, we'll leave that as it is. 111 00:06:19.300 --> 00:06:21.030 Now, if we run that again. 112 00:06:21.030 --> 00:06:21.760 Tim, 15. 113 00:06:21.760 --> 00:06:25.490 We get the else only, please come back in three years. 114 00:06:25.490 --> 00:06:27.800 But if actually run it again, and type in Tim. 115 00:06:27.800 --> 00:06:28.590 21. 116 00:06:28.590 --> 00:06:32.510 You're old enough to vote, please put an X in the box. 117 00:06:32.510 --> 00:06:36.050 So basically, the code that's in this block of code, 118 00:06:36.050 --> 00:06:39.280 all of that is going to be executed if this condition above it is true. 119 00:06:39.280 --> 00:06:41.460 So likewise, we could've done a similar thing for the else. 120 00:06:41.460 --> 00:06:43.000 We could put extra code down here, and 121 00:06:43.000 --> 00:06:46.420 provided it's indented correctly, that's going to run, 122 00:06:46.420 --> 00:06:50.070 if that else condition is actually true in this case, if h was less than 18. 123 00:06:50.070 --> 00:06:55.660 Now, sometimes you might need more than 2 cases, so what Python does, 124 00:06:55.660 --> 00:07:00.715 it let's us test conditions by letting us the L if which is an abbreviation for. 125 00:07:00.715 --> 00:07:03.750 So, let's actually come up with another example, and move on from this one, 126 00:07:03.750 --> 00:07:06.140 so I'm going to comment that out. 127 00:07:06.140 --> 00:07:06.820 And I've commented on that. 128 00:07:06.820 --> 00:07:08.950 You noticed that at the bottom it actually tells you the command I used. 129 00:07:08.950 --> 00:07:12.470 Command slash which is control slash on Windows or 130 00:07:12.470 --> 00:07:15.980 Linux, and that gives you a way to actually comment out a locator once. 131 00:07:15.980 --> 00:07:18.760 So, if you want to instead of just deleting something instead of 132 00:07:18.760 --> 00:07:22.000 comment it out, so that it's not going to be executed, that's a way of doing it. 133 00:07:22.000 --> 00:07:23.150 Let's type another example in. 134 00:07:23.150 --> 00:07:28.622 I'm gonna print, please guess a number, 135 00:07:28.622 --> 00:07:31.360 between 1and 10. 136 00:07:31.360 --> 00:07:36.230 We're going to set guess, the variable guess, 137 00:07:36.230 --> 00:07:42.640 to convert the input to an int, and get that number from the keyboard. 138 00:07:42.640 --> 00:07:45.240 Now, we're going to do some testing using the if statements. 139 00:07:45.240 --> 00:07:50.640 So, we're going to put if, guess is less than 5, print. 140 00:07:52.670 --> 00:07:54.160 Please guess, higher. 141 00:07:56.490 --> 00:07:59.730 Guess equals input, and 142 00:07:59.730 --> 00:08:04.420 we need to add a [INAUDIBLE] to make it valid, converting it to a number, so 143 00:08:04.420 --> 00:08:10.000 we can do these if comparisons, and then, we're gonna put if guess is equal to 5. 144 00:08:10.000 --> 00:08:13.890 Now, notice when I'm putting equal to 5, I've had to put two equals. 145 00:08:13.890 --> 00:08:18.200 If you use one equal sign, like the line above, guess equals int, 146 00:08:18.200 --> 00:08:21.410 we're actually setting the value for the guess variable. 147 00:08:21.410 --> 00:08:24.070 But two equals together, that's actually doing a test. 148 00:08:24.070 --> 00:08:29.320 We're actually saying, does the guess variable have a value of 5 to it. 149 00:08:29.320 --> 00:08:34.378 So, if that's the case we're going to put another colon, 150 00:08:34.378 --> 00:08:38.740 press enter, print, well done, you guessed it. 151 00:08:38.740 --> 00:08:43.590 And pressing backspace again, else print, 152 00:08:45.310 --> 00:08:48.620 sorry you have not guessed correctly. 153 00:08:50.820 --> 00:08:52.890 So, I can see what's happened there, if your guess is less than five, 154 00:08:52.890 --> 00:08:54.890 you're gonna print please guess higher. 155 00:08:54.890 --> 00:08:59.430 If your guess equals input, if your guess equals five, print well done, 156 00:08:59.430 --> 00:09:00.360 you've guessed it. 157 00:09:00.360 --> 00:09:02.790 Else, print sorry, you have not guessed correctly. 158 00:09:02.790 --> 00:09:06.663 But, what we're also gonna do is we're going to embed that, so 159 00:09:06.663 --> 00:09:11.687 we're going to press backspace, backspace, type elif which stands for else if, 160 00:09:11.687 --> 00:09:16.250 guess greater than five, you're gonna type print, please guess lower. 161 00:09:19.511 --> 00:09:20.770 Guess equals input. 162 00:09:20.770 --> 00:09:22.300 Guess equals int input. 163 00:09:24.280 --> 00:09:30.550 If guess is equal to five, the two equals again, print, well done. 164 00:09:30.550 --> 00:09:31.050 You guessed it. 165 00:09:34.711 --> 00:09:43.360 Else, print sorry you have not guessed correctly. 166 00:09:43.360 --> 00:09:48.300 And so, and informally, and here we can put else, 167 00:09:49.790 --> 00:09:53.650 print you got it this time. 168 00:09:55.370 --> 00:09:58.480 So, you can see we've got quite a few ifs there, and 169 00:09:58.480 --> 00:10:01.410 an elif, an else if and then, an else down at the bottom. 170 00:10:01.410 --> 00:10:04.770 So, what's actually happening here, we're doing if the guess, that the first number 171 00:10:04.770 --> 00:10:09.330 that they typed is less than five, then it will go into, and execute this code. 172 00:10:09.330 --> 00:10:11.610 Otherwise, getting back to the start again, that wasn't true, 173 00:10:11.610 --> 00:10:12.210 so in other words, 174 00:10:12.210 --> 00:10:16.020 what they type was greater than five, then this code down here will be executed. 175 00:10:16.020 --> 00:10:18.350 We'll do a test here to see whether the guess is greater than five, 176 00:10:18.350 --> 00:10:20.550 and if it was it's going to execute that code. 177 00:10:20.550 --> 00:10:25.170 And finally, the third level, else, will be if you typed five for 178 00:10:25.170 --> 00:10:26.950 the first time when you get that error, or 179 00:10:26.950 --> 00:10:29.800 get that message to say that you got it right the first time. 180 00:10:29.800 --> 00:10:32.950 So, the indentation level is very important to actually get an idea, 181 00:10:32.950 --> 00:10:36.090 of what's actually working because this level of if, 182 00:10:36.090 --> 00:10:38.240 is completely different to this level. 183 00:10:38.240 --> 00:10:40.100 This second level was only going to be executed. 184 00:10:40.100 --> 00:10:44.140 The codes only going to go in there, if this condition was true. 185 00:10:44.140 --> 00:10:45.820 So, let's actually run it using that code, 186 00:10:45.820 --> 00:10:49.300 and see how the code's actually going to work. 187 00:10:49.300 --> 00:10:52.420 So, let's do the first test, and we type in the number that's exactly five. 188 00:10:54.530 --> 00:10:55.390 You got it first time. 189 00:10:55.390 --> 00:10:58.790 So, what happened was, this code was executed, guess is less than five, 190 00:10:58.790 --> 00:11:03.160 and that didn't happen, because guess was equal to five it's not less than five. 191 00:11:03.160 --> 00:11:07.260 So, the computer jumps into this line of code, and says okay, was the guess that 192 00:11:07.260 --> 00:11:11.440 was entered greater than five, or no in this case it wasn't because we typed five. 193 00:11:11.440 --> 00:11:12.960 So therefore, it jumped to the third one, 194 00:11:12.960 --> 00:11:16.420 which was else, and printed that list, cuz there wasn't a condition. 195 00:11:16.420 --> 00:11:20.590 Else, without any statement means just actually print whatever else is there. 196 00:11:20.590 --> 00:11:21.700 So, running it again. 197 00:11:21.700 --> 00:11:24.270 This time if we type a number, let's say typing a number that's 4. 198 00:11:24.270 --> 00:11:29.130 And it says, Please guess higher, so you can see, is guess less than 5? 199 00:11:29.130 --> 00:11:30.100 Well yes, cuz it's 4. 200 00:11:30.100 --> 00:11:33.350 If guess is equal to 5, print Well done, you guessed it. 201 00:11:33.350 --> 00:11:36.370 So if we type 5 in here now it should come up and say, Well done, you guessed it. 202 00:11:36.370 --> 00:11:38.850 But if we run it again, and 203 00:11:38.850 --> 00:11:43.330 put four, please get's higher, this time we're going to guess 890. 204 00:11:43.330 --> 00:11:46.650 Sorry you've not guess correctly. 205 00:11:46.650 --> 00:11:51.830 And lastly, we'll do a test and we'll say, we'll start with ten, please guess lower. 206 00:11:51.830 --> 00:11:53.090 Your top five. 207 00:11:53.090 --> 00:11:55.470 Well now, you've guessed it. 208 00:11:55.470 --> 00:11:56.180 And run it again. 209 00:11:56.180 --> 00:11:58.720 12, please guess lower. 210 00:11:58.720 --> 00:12:01.470 11, sorry you've not guessed correctly. 211 00:12:01.470 --> 00:12:05.860 So, you can see, I hope you're now getting an idea about how you can actually, 212 00:12:05.860 --> 00:12:09.880 how this code works, and how blocks can be quite complex including further if and 213 00:12:09.880 --> 00:12:14.340 else blocks, and much more contained within it, you saw how when testing for 214 00:12:14.340 --> 00:12:16.230 equality we don't use a single equal symbol. 215 00:12:16.230 --> 00:12:20.290 We use the two symbols to actually check to see whether value has a certain value. 216 00:12:20.290 --> 00:12:24.670 And of course a single equal is if you're assigning a value as in this line here. 217 00:12:24.670 --> 00:12:29.060 Assigning the variable guess, the value of whatever of was 218 00:12:29.060 --> 00:12:31.860 typed by the user converting it automatically to a number. 219 00:12:31.860 --> 00:12:36.070 Now, and if statement can also include many else ifs, elifs, and as you can see, 220 00:12:36.070 --> 00:12:36.870 I've already used one there. 221 00:12:36.870 --> 00:12:39.510 I've already used one elif, and then at the last line, 222 00:12:39.510 --> 00:12:43.730 we used an else, which is sorta the final test that we can actually have. 223 00:12:43.730 --> 00:12:46.350 If all else files, it's gonna go to that part of the code. 224 00:12:46.350 --> 00:12:49.830 Now, there are much better ways of writing code than what we've shown here, 225 00:12:49.830 --> 00:12:51.650 this is really just an example to get you going. 226 00:12:51.650 --> 00:12:54.650 And you will see better ways of doing this shortly. 227 00:12:54.650 --> 00:12:57.730 One of the bad things that we've done here is we're actually duplicated code, so 228 00:12:57.730 --> 00:12:59.470 we've actually added this code here for 229 00:12:59.470 --> 00:13:04.160 example, we've duplicated there, and we've duplicated it there. 230 00:13:04.160 --> 00:13:06.740 And we wouldn't want to do that in a real-world scenario, but this again, 231 00:13:06.740 --> 00:13:08.140 just to get us going. 232 00:13:08.140 --> 00:13:10.290 If you ever see something like this when you're programming though, 233 00:13:10.290 --> 00:13:13.420 that's probably a good indication that the code that you're working on 234 00:13:13.420 --> 00:13:16.560 could be improved or reducing that duplication. 235 00:13:16.560 --> 00:13:20.800 So, we saw that in a previous bit of code this one up here, greater than or equal, 236 00:13:20.800 --> 00:13:23.270 you can actually test for that using a greater than or 237 00:13:23.270 --> 00:13:24.700 equal sign as you saw there. 238 00:13:24.700 --> 00:13:27.410 We can test for equality with the two equals. 239 00:13:27.410 --> 00:13:28.070 We can test for 240 00:13:28.070 --> 00:13:31.290 greater than which is the greater than sign likewise with the less than. 241 00:13:31.290 --> 00:13:33.840 And there's a couple of other ones as well. 242 00:13:33.840 --> 00:13:34.860 which we'll see as we go through. 243 00:13:34.860 --> 00:13:37.050 There's a not equal which would be similar to that. 244 00:13:37.050 --> 00:13:38.130 Which would be like this. 245 00:13:38.130 --> 00:13:40.620 If you want to check something that is not equal to. 246 00:13:40.620 --> 00:13:44.170 And there's also a less than and equal which works just like a greater than does, 247 00:13:44.170 --> 00:13:46.570 but obviously, it's the other side comparison, 248 00:13:46.570 --> 00:13:50.870 making sure our value is either less than or equal to that value. 249 00:13:50.870 --> 00:13:53.690 So now, we actually know how to actually check for 250 00:13:53.690 --> 00:13:57.500 nautical as I showed you briefly, by doing something like that, 251 00:13:57.500 --> 00:13:59.870 we can actually change our code, and make it a lot more efficient. 252 00:13:59.870 --> 00:14:01.300 So, what we could actually do here is we could, 253 00:14:01.300 --> 00:14:05.860 instead of putting if guess is less than 5, we could put if guess not equal to 5. 254 00:14:05.860 --> 00:14:11.030 And the next time we could put if guess less than 5, 255 00:14:11.030 --> 00:14:14.865 and then, we could indent that to put please print higher. 256 00:14:14.865 --> 00:14:22.291 Else, print please guess lower. 257 00:14:24.220 --> 00:14:25.680 Please, guess lower. 258 00:14:27.390 --> 00:14:31.430 And what we can do then, is we can put guess equals int, input. 259 00:14:34.080 --> 00:14:39.364 Then, put if guess, is equal to 5, 260 00:14:39.364 --> 00:14:44.307 print, well done you guessed it, 261 00:14:44.307 --> 00:14:48.058 else, print("Sorry, 262 00:14:48.058 --> 00:14:53.521 you have not guessed correctly"). 263 00:14:55.861 --> 00:14:59.440 And then, removing this else if now, the elif, which we no longer need. 264 00:14:59.440 --> 00:15:02.000 Deleting that, we can actually just print("You got it first time"). 265 00:15:02.000 --> 00:15:04.120 So, you can see we've now consolidated that code. 266 00:15:04.120 --> 00:15:06.260 We're doing a test at the start. 267 00:15:06.260 --> 00:15:08.570 So, if guest is not equal to 5, 268 00:15:08.570 --> 00:15:13.700 so if any value other than 5 was entered, we're gonna start processing down here. 269 00:15:13.700 --> 00:15:16.000 But if 5 was entered, we're gonna go straight down to here, 270 00:15:16.000 --> 00:15:17.760 just how you got it first time. 271 00:15:17.760 --> 00:15:21.500 They were doing a test say if guess is less than five, the message would say, 272 00:15:21.500 --> 00:15:24.460 please guess higher, because we know the number needs to be higher. 273 00:15:24.460 --> 00:15:27.980 Else, at this point the number has to be higher, so we print a message that says, 274 00:15:27.980 --> 00:15:29.470 please guess lower. 275 00:15:29.470 --> 00:15:33.290 And will ask for an input again, and if the guess is correct, and equal to 5, 276 00:15:33.290 --> 00:15:36.510 we print a message that say, what we've done you guessed it, should be guessed. 277 00:15:37.790 --> 00:15:40.840 Otherwise, we put in summary you've not guessed correctly, and 278 00:15:40.840 --> 00:15:43.670 this funnel else, is only called at this level 279 00:15:43.670 --> 00:15:46.000 which would be if they actually got it right the first time. 280 00:15:46.000 --> 00:15:48.820 So, let's just run that, and confirm that it still works. 281 00:15:48.820 --> 00:15:50.230 So, I enter number 5 first. 282 00:15:50.230 --> 00:15:51.510 He got it right, first time. 283 00:15:51.510 --> 00:15:55.780 So, the first test to see whether it's not equal to 5 doesn't go into there, so 284 00:15:55.780 --> 00:15:58.090 therefore, it has to be equal to 5 because we've tested for 285 00:15:58.090 --> 00:16:01.490 not equal here, so we're getting that message correctly, gonna run this again, 286 00:16:01.490 --> 00:16:05.780 went to the number four, please gets higher. 287 00:16:05.780 --> 00:16:06.750 Which you would expect it to do. 288 00:16:06.750 --> 00:16:10.260 Now, when I try another number this time we'll say six, sorry it could not guess 289 00:16:10.260 --> 00:16:15.940 correctly, run it again four, this time we'll actually guess five. 290 00:16:15.940 --> 00:16:19.300 Well, now you've guessed it, and that's because this bit of code was executed 291 00:16:19.300 --> 00:16:22.600 because the second time we actually asked for the input which was there. 292 00:16:22.600 --> 00:16:24.860 Just doing another test to see whether we've got it right. 293 00:16:24.860 --> 00:16:28.200 And finally, we'll hit a number like eight. 294 00:16:28.200 --> 00:16:29.470 Please guess lower. 295 00:16:29.470 --> 00:16:31.620 Five. Well, now you've guessed it, and 296 00:16:31.620 --> 00:16:33.480 the last one we'll actually do a test for eight. 297 00:16:35.190 --> 00:16:38.200 And so six, sorry you have not guessed it correctly. 298 00:16:38.200 --> 00:16:41.140 So, you can see this really highlights the importance of indenting your 299 00:16:41.140 --> 00:16:44.530 code in Python, because the second guess, this one here, 300 00:16:44.530 --> 00:16:48.880 is not at all dependent on this other code that's in this first block, so it's really 301 00:16:48.880 --> 00:16:52.920 important to structure your code, to put your code in the right indentation level, 302 00:16:52.920 --> 00:16:57.200 and that helps you ensure that you're executing the code in the right scenarios. 303 00:16:57.200 --> 00:17:01.320 So, for example, here that's only going to execute is guests is less than five. 304 00:17:01.320 --> 00:17:04.370 This line's only going to be executed if guests is greater than five, 305 00:17:04.370 --> 00:17:09.020 but in this case, here, this code is going to be executed no matter what, 306 00:17:09.020 --> 00:17:11.630 because we haven't got any tests, and it's not indented to the next level. 307 00:17:11.630 --> 00:17:15.030 Indented, it's going to be, running the code at the same level, if you will. 308 00:17:15.030 --> 00:17:16.290 One of the other things we could've done there. 309 00:17:16.290 --> 00:17:23.010 We could've done something like # guest must be greater than 5. 310 00:17:23.010 --> 00:17:27.400 Which will tell us that, because in this code here we can see that, okay, 311 00:17:27.400 --> 00:17:29.150 that's at less than 5 test. 312 00:17:29.150 --> 00:17:32.110 Sometimes with just an else it can be a good idea to put a comment, 313 00:17:32.110 --> 00:17:35.880 just to spell out what the scenario is, what this else is gonna be called for. 314 00:17:35.880 --> 00:17:39.330 In this case It's only going to be executed if guess is greater than five, so 315 00:17:39.330 --> 00:17:41.180 that can be a good place to put a comment, 316 00:17:41.180 --> 00:17:43.100 just to confirm your understanding of the actual code. 317 00:17:43.100 --> 00:17:47.330 And it also helps yourself certainly, but also other programmers in the future, 318 00:17:47.330 --> 00:17:49.170 who are actually reading that to figure out what's going on. 319 00:17:49.170 --> 00:17:53.330 And again, just a reminder that comments are ignored by the Python interpreter, and 320 00:17:53.330 --> 00:17:55.470 they're there purely to document your code. 321 00:17:55.470 --> 00:17:58.270 So generally, if you use comments to explain why you've done something in 322 00:17:58.270 --> 00:18:02.540 a certain way, or as a reminder of certain conditions, or variable states, 323 00:18:02.540 --> 00:18:05.270 that may not be obvious to someone who's reading it for the first time. 324 00:18:05.270 --> 00:18:07.370 And remember that, this is very important in comments, 325 00:18:07.370 --> 00:18:09.520 if you come back to tomorrow for a code weeks or 326 00:18:09.520 --> 00:18:13.270 months after you wrote it, that documentation can really come in handy. 327 00:18:13.270 --> 00:18:16.410 And just a final thing about comments before we end this video is that 328 00:18:16.410 --> 00:18:18.920 Python does provide a way to have multiline comments, and 329 00:18:18.920 --> 00:18:22.380 we'll be covering that in the section on documentation, and doc strings, and 330 00:18:22.380 --> 00:18:23.880 you can find out more about it there. 331 00:18:23.880 --> 00:18:26.730 For now, if you want a comment to span several lines just start each line 332 00:18:26.730 --> 00:18:27.640 with a hash. 333 00:18:27.640 --> 00:18:29.970 That you've seen at the top of this code here now. 334 00:18:29.970 --> 00:18:31.710 Okay, so we're gonna end this video here now. 335 00:18:31.710 --> 00:18:34.610 When we come back in the next video, we're gonna start looking at 336 00:18:34.610 --> 00:18:37.870 more complicated conditions than the simple ones that we've used so far, and 337 00:18:37.870 --> 00:18:42.380 show you how Python provides the means to produce quite complex expressions.