1
00:00:05,300 --> 00:00:08,660
So, in the previous video, we started
talking a little bit about program flow.

2
00:00:08,660 --> 00:00:12,580
So, let's now continue on with that, and
actually talk about the if statement.

3
00:00:12,580 --> 00:00:15,810
So, what I've done is,
I've created a new project in IntelliJ.

4
00:00:15,810 --> 00:00:17,750
And I've called it ifprogramflow.

5
00:00:17,750 --> 00:00:22,630
And I'm going to right-click that now,
click on New, select Python File, and

6
00:00:22,630 --> 00:00:24,450
I'm just gonna call it ifprogramflow.

7
00:00:26,400 --> 00:00:28,370
Making sure that I'm
actually clicked in there.

8
00:00:28,370 --> 00:00:32,210
Okay, so we've seen how we can
store values in variables,

9
00:00:32,210 --> 00:00:33,830
and we can then, print them out.

10
00:00:33,830 --> 00:00:36,630
And we've also looked at how to
get input from the keyboard.

11
00:00:36,630 --> 00:00:39,380
So, we now start getting
Python to make decisions

12
00:00:39,380 --> 00:00:43,250
based on the values of variables, and
the input received from the keyboard.

13
00:00:43,250 --> 00:00:45,420
As a quick recap,
just from the following program,

14
00:00:45,420 --> 00:00:47,340
just to see where we were
at in the previous video.

15
00:00:47,340 --> 00:00:49,340
So, we can type name Equals input.

16
00:00:52,920 --> 00:00:54,480
Please enter your name.

17
00:00:57,180 --> 00:00:58,160
Now, we can type age equals input.

18
00:01:00,880 --> 00:01:06,270
How old are you, placement field, and

19
00:01:06,270 --> 00:01:13,880
we're going to put format name,
you print age.

20
00:01:13,880 --> 00:01:16,450
So, notice how we can apply
formatting to any string,

21
00:01:16,450 --> 00:01:20,160
even the ones that we're going to be
using as a prompt to the input function.

22
00:01:20,160 --> 00:01:24,180
So, in the first line we've
created a variable called name.

23
00:01:24,180 --> 00:01:27,960
And we're going to accept the name
that's been entered by the user

24
00:01:27,960 --> 00:01:31,730
from the keyboard, and
put that into the variable called name.

25
00:01:31,730 --> 00:01:35,600
Then, the next line, line three,
we've got an age, we'll ask it to,

26
00:01:35,600 --> 00:01:39,190
for the user to put how old they are,
and we're using a replacement field,

27
00:01:39,190 --> 00:01:42,500
to actually format, and print the name
that was entered on line two, and

28
00:01:42,500 --> 00:01:47,580
share that on the string, on the screen,
along with the prompt to enter their age,

29
00:01:47,580 --> 00:01:50,100
and on the fourth line, line four,
we actually printed out.

30
00:01:50,100 --> 00:01:53,580
So, if we run this,
please enter your name.

31
00:01:53,580 --> 00:01:56,170
Tim.
How old are you Tim?

32
00:01:56,170 --> 00:01:59,750
I'll say I'm 21, I certainly wish
that I was Press Enter > 21.

33
00:01:59,750 --> 00:02:03,110
So, we can say that's
a bit of a recap we're at.

34
00:02:03,110 --> 00:02:06,370
Now, the input function
returns a string data type.

35
00:02:06,370 --> 00:02:08,509
So, in order to actually convert that 21.

36
00:02:08,509 --> 00:02:11,860
That number that I actually
typed into a number.

37
00:02:11,860 --> 00:02:14,590
If we want age to represent a number,
rather than a string,

38
00:02:14,590 --> 00:02:17,130
we need to actually convert it.

39
00:02:17,130 --> 00:02:19,400
We do that using the int function.

40
00:02:19,400 --> 00:02:22,460
Before making some decisions
based on the person's age.

41
00:02:22,460 --> 00:02:26,130
So, what we can do is actually
change this, to put it int,

42
00:02:26,130 --> 00:02:29,840
which again converts,
what's been typed into a stream.

43
00:02:29,840 --> 00:02:30,440
Then, we actually

44
00:02:30,440 --> 00:02:32,640
have to add a bracket right on the end
of the line to make that valid.

45
00:02:32,640 --> 00:02:37,160
When we go to run it, we will see
that there aren't any differences, so

46
00:02:37,160 --> 00:02:38,430
it will do that same thing.

47
00:02:38,430 --> 00:02:44,410
The only thing is we've changed it now, so
that the data type, because we've selected

48
00:02:44,410 --> 00:02:48,650
the int, it's going to be a number Instead
of what it was before which was a string.

49
00:02:48,650 --> 00:02:51,420
So now, that we've done that what
we can do is we can actually get

50
00:02:51,420 --> 00:02:55,570
the computer to actually try, and
do some processing based on that age.

51
00:02:55,570 --> 00:03:02,010
So, lets do that, so I'm gonna put if,
if age is greater than or equal to,

52
00:03:02,010 --> 00:03:06,210
that's what those two symbols are that
means it greater than or equal to 18.

53
00:03:06,210 --> 00:03:08,990
Then we put a colon and
we press Enter to go to the next line.

54
00:03:08,990 --> 00:03:13,350
Then we're going to print ("You
are old enough to vote.").

55
00:03:13,350 --> 00:03:16,010
So, if we run that.

56
00:03:18,710 --> 00:03:21,260
Before we do that, let's actually type
in something that's not a number, and

57
00:03:21,260 --> 00:03:22,670
see what happens.

58
00:03:22,670 --> 00:03:24,200
We're gonna type in the words twenty one.

59
00:03:24,200 --> 00:03:26,190
We actually got an error.

60
00:03:27,340 --> 00:03:30,570
And the error is that it actually,
basically just is expecting a number, but

61
00:03:30,570 --> 00:03:33,270
it actually receives some text, and
it's got a little bit confused.

62
00:03:33,270 --> 00:03:37,630
So, keep that in mind, that if you specify
something like we've done here, and

63
00:03:37,630 --> 00:03:40,070
an input,
it's gonna assume that that is the case.

64
00:03:40,070 --> 00:03:42,750
But don't worry too much about that,
because we're gonna be covering

65
00:03:42,750 --> 00:03:45,410
error handling, and
input validation in the course.

66
00:03:45,410 --> 00:03:46,910
But for now, to be valid,

67
00:03:46,910 --> 00:03:49,230
we need to make sure that we're
only entering valid numbers.

68
00:03:49,230 --> 00:03:50,310
So, we're gonna go back, and do it again.

69
00:03:50,310 --> 00:03:50,940
Tim?

70
00:03:50,940 --> 00:03:55,820
Twenty-one, and you can see we've got
the message printed "You are old enough

71
00:03:55,820 --> 00:03:59,340
to vote." And, once we've got
the age in the numeric format,

72
00:03:59,340 --> 00:04:02,630
we can actually use the greater than,
and the equal, as we've used here,

73
00:04:02,630 --> 00:04:05,700
to actually check to see whether
it is greater or equal than 18.

74
00:04:05,700 --> 00:04:09,180
And we're doing that using an if
statement, with a condition to check.

75
00:04:09,180 --> 00:04:13,310
So, if the condition is true, in this case
the age was greater than, or equal to 18,

76
00:04:13,310 --> 00:04:16,170
then, we're printing the message
that you're old enough to vote.

77
00:04:16,170 --> 00:04:19,519
If the age is less than 18,
nothing happens.

78
00:04:19,519 --> 00:04:21,140
It actually would go into the next line.

79
00:04:21,140 --> 00:04:24,070
Now, that's a little but unfriendly,
now what we can do we can code you for

80
00:04:24,070 --> 00:04:27,160
that we can put in
something called an else.

81
00:04:27,160 --> 00:04:31,710
Press back, press space will
delete press else or top else.

82
00:04:31,710 --> 00:04:35,170
.
And press Enter.

83
00:04:35,170 --> 00:04:36,500
Print.

84
00:04:36,500 --> 00:04:42,000
Please come back in, and
we're gonna put our replacement field.

85
00:04:42,000 --> 00:04:44,860
Let's use .format.

86
00:04:44,860 --> 00:04:49,110
Let's actually get the computer to decide,
how many years I've gotta come back with.

87
00:04:49,110 --> 00:04:49,690
And we can do that.

88
00:04:49,690 --> 00:04:52,480
We can actually put the number
of years which was 18,

89
00:04:52,480 --> 00:04:56,140
less the current age that was typed in,
so 18 take whatever.

90
00:04:56,140 --> 00:05:00,030
So, if I've actually typed in the age
of 15, it would be 18 take 15 is 3.

91
00:05:00,030 --> 00:05:03,460
So, if we actually run that now, firstly,

92
00:05:03,460 --> 00:05:07,690
I'm putting my name, I'll put 21 we still
get the same message, and notice how this

93
00:05:07,690 --> 00:05:12,450
line here is basically ignored, and
that's because the first part was correct.

94
00:05:12,450 --> 00:05:16,360
So, how the program works is, it comes to,
and looks for this condition, and

95
00:05:16,360 --> 00:05:20,580
if that is true, in other words, in this
case what was typed in is greater than or

96
00:05:20,580 --> 00:05:23,760
equal to 18, then that line is executed.

97
00:05:23,760 --> 00:05:27,840
And then, that line is executed, not
the next line with the else isn't it, and

98
00:05:27,840 --> 00:05:33,090
then, the actual code jumps past the else,
and the print and goes on to next part.

99
00:05:33,090 --> 00:05:37,760
But otherwise, so if the age entered
isn't greater than a towards at 17 or

100
00:05:37,760 --> 00:05:39,780
less, we get the second part.

101
00:05:39,780 --> 00:05:41,370
And just to confirm that,
we can run that again.

102
00:05:42,980 --> 00:05:46,720
Tim, if I put in 15,
please come back in three.

103
00:05:46,720 --> 00:05:50,390
And I should go back to change that,
and put that in years, like so.

104
00:05:50,390 --> 00:05:54,250
If we actually run that again, Tim 15.

105
00:05:54,250 --> 00:05:57,450
Please come back in three years.

106
00:05:57,450 --> 00:06:01,600
Now, the other thing to note
here is the block of code

107
00:06:01,600 --> 00:06:05,920
that's within these conditions can have
more than one line of code to execute.

108
00:06:05,920 --> 00:06:10,970
So, in other words we can come back, and
put, you're old enough to vote, print,

109
00:06:12,170 --> 00:06:20,750
please put an X in the ballot box or
more correctly print x in the box.

110
00:06:20,750 --> 00:06:22,390
Else, we'll leave that as it is.

111
00:06:22,390 --> 00:06:24,120
Now, if we run that again.

112
00:06:24,120 --> 00:06:24,850
Tim, 15.

113
00:06:24,850 --> 00:06:28,580
We get the else only,
please come back in three years.

114
00:06:28,580 --> 00:06:30,890
But if actually run it again,
and type in Tim.

115
00:06:30,890 --> 00:06:31,680
21.

116
00:06:31,680 --> 00:06:35,600
You're old enough to vote,
please put an X in the box.

117
00:06:35,600 --> 00:06:39,140
So basically,
the code that's in this block of code,

118
00:06:39,140 --> 00:06:42,370
all of that is going to be executed
if this condition above it is true.

119
00:06:42,370 --> 00:06:44,550
So likewise, we could've done
a similar thing for the else.

120
00:06:44,550 --> 00:06:46,090
We could put extra code down here, and

121
00:06:46,090 --> 00:06:49,510
provided it's indented correctly,
that's going to run,

122
00:06:49,510 --> 00:06:53,160
if that else condition is actually true
in this case, if h was less than 18.

123
00:06:53,160 --> 00:06:58,750
Now, sometimes you might need more
than 2 cases, so what Python does,

124
00:06:58,750 --> 00:07:03,800
it let's us test conditions by letting us
the L if which is an abbreviation for.

125
00:07:03,800 --> 00:07:06,840
So, let's actually come up with another
example, and move on from this one,

126
00:07:06,840 --> 00:07:09,230
so I'm going to comment that out.

127
00:07:09,230 --> 00:07:09,910
And I've commented on that.

128
00:07:09,910 --> 00:07:12,040
You noticed that at the bottom it
actually tells you the command I used.

129
00:07:12,040 --> 00:07:15,560
Command slash which is
control slash on Windows or

130
00:07:15,560 --> 00:07:19,070
Linux, and that gives you a way to
actually comment out a locator once.

131
00:07:19,070 --> 00:07:21,850
So, if you want to instead of just
deleting something instead of

132
00:07:21,850 --> 00:07:25,090
comment it out, so that it's not going to
be executed, that's a way of doing it.

133
00:07:25,090 --> 00:07:26,240
Let's type another example in.

134
00:07:26,240 --> 00:07:31,710
I'm gonna print, please guess a number,

135
00:07:31,710 --> 00:07:34,450
between 1and 10.

136
00:07:34,450 --> 00:07:39,320
We're going to set guess,
the variable guess,

137
00:07:39,320 --> 00:07:45,730
to convert the input to an int, and
get that number from the keyboard.

138
00:07:45,730 --> 00:07:48,330
Now, we're going to do some
testing using the if statements.

139
00:07:48,330 --> 00:07:53,730
So, we're going to put if,
guess is less than 5, print.

140
00:07:55,760 --> 00:07:57,250
Please guess, higher.

141
00:07:59,580 --> 00:08:02,820
Guess equals input, and

142
00:08:02,820 --> 00:08:07,510
we need to add a [INAUDIBLE] to make
it valid, converting it to a number, so

143
00:08:07,510 --> 00:08:13,090
we can do these if comparisons, and then,
we're gonna put if guess is equal to 5.

144
00:08:13,090 --> 00:08:16,980
Now, notice when I'm putting equal to 5,
I've had to put two equals.

145
00:08:16,980 --> 00:08:21,290
If you use one equal sign,
like the line above, guess equals int,

146
00:08:21,290 --> 00:08:24,500
we're actually setting the value for
the guess variable.

147
00:08:24,500 --> 00:08:27,160
But two equals together,
that's actually doing a test.

148
00:08:27,160 --> 00:08:32,409
We're actually saying, does the guess
variable have a value of 5 to it.

149
00:08:32,409 --> 00:08:37,460
So, if that's the case we're
going to put another colon,

150
00:08:37,460 --> 00:08:41,830
press enter, print,
well done, you guessed it.

151
00:08:41,830 --> 00:08:46,680
And pressing backspace again, else print,

152
00:08:48,400 --> 00:08:51,710
sorry you have not guessed correctly.

153
00:08:53,910 --> 00:08:55,980
So, I can see what's happened there,
if your guess is less than five,

154
00:08:55,980 --> 00:08:57,980
you're gonna print please guess higher.

155
00:08:57,980 --> 00:09:02,520
If your guess equals input, if your
guess equals five, print well done,

156
00:09:02,520 --> 00:09:03,450
you've guessed it.

157
00:09:03,450 --> 00:09:05,880
Else, print sorry,
you have not guessed correctly.

158
00:09:05,880 --> 00:09:09,750
But, what we're also gonna do
is we're going to embed that, so

159
00:09:09,750 --> 00:09:14,770
we're going to press backspace, backspace,
type elif which stands for else if,

160
00:09:14,770 --> 00:09:19,340
guess greater than five, you're gonna
type print, please guess lower.

161
00:09:22,600 --> 00:09:23,860
Guess equals input.

162
00:09:23,860 --> 00:09:25,390
Guess equals int input.

163
00:09:27,370 --> 00:09:33,640
If guess is equal to five,
the two equals again, print, well done.

164
00:09:33,640 --> 00:09:34,140
You guessed it.

165
00:09:37,800 --> 00:09:46,450
Else, print sorry you have
not guessed correctly.

166
00:09:46,450 --> 00:09:51,390
And so, and informally,
and here we can put else,

167
00:09:52,880 --> 00:09:56,740
print you got it this time.

168
00:09:58,460 --> 00:10:01,570
So, you can see we've got
quite a few ifs there, and

169
00:10:01,570 --> 00:10:04,500
an elif, an else if and then,
an else down at the bottom.

170
00:10:04,500 --> 00:10:07,860
So, what's actually happening here, we're
doing if the guess, that the first number

171
00:10:07,860 --> 00:10:12,420
that they typed is less than five, then
it will go into, and execute this code.

172
00:10:12,420 --> 00:10:14,700
Otherwise, getting back to
the start again, that wasn't true,

173
00:10:14,700 --> 00:10:15,300
so in other words,

174
00:10:15,300 --> 00:10:19,110
what they type was greater than five,
then this code down here will be executed.

175
00:10:19,110 --> 00:10:21,440
We'll do a test here to see whether
the guess is greater than five,

176
00:10:21,440 --> 00:10:23,640
and if it was it's going
to execute that code.

177
00:10:23,640 --> 00:10:28,260
And finally, the third level, else,
will be if you typed five for

178
00:10:28,260 --> 00:10:30,040
the first time when you get that error, or

179
00:10:30,040 --> 00:10:32,890
get that message to say that you
got it right the first time.

180
00:10:32,890 --> 00:10:36,040
So, the indentation level is very
important to actually get an idea,

181
00:10:36,040 --> 00:10:39,180
of what's actually working
because this level of if,

182
00:10:39,180 --> 00:10:41,330
is completely different to this level.

183
00:10:41,330 --> 00:10:43,190
This second level was only
going to be executed.

184
00:10:43,190 --> 00:10:47,230
The codes only going to go in there,
if this condition was true.

185
00:10:47,230 --> 00:10:48,910
So, let's actually run it using that code,

186
00:10:48,910 --> 00:10:52,390
and see how the code's
actually going to work.

187
00:10:52,390 --> 00:10:55,510
So, let's do the first test, and
we type in the number that's exactly five.

188
00:10:57,620 --> 00:10:58,480
You got it first time.

189
00:10:58,480 --> 00:11:01,880
So, what happened was, this code was
executed, guess is less than five,

190
00:11:01,880 --> 00:11:06,250
and that didn't happen, because guess was
equal to five it's not less than five.

191
00:11:06,250 --> 00:11:10,350
So, the computer jumps into this line of
code, and says okay, was the guess that

192
00:11:10,350 --> 00:11:14,530
was entered greater than five, or no in
this case it wasn't because we typed five.

193
00:11:14,530 --> 00:11:16,050
So therefore, it jumped to the third one,

194
00:11:16,050 --> 00:11:19,510
which was else, and printed that list,
cuz there wasn't a condition.

195
00:11:19,510 --> 00:11:23,680
Else, without any statement means just
actually print whatever else is there.

196
00:11:23,680 --> 00:11:24,790
So, running it again.

197
00:11:24,790 --> 00:11:27,360
This time if we type a number,
let's say typing a number that's 4.

198
00:11:27,360 --> 00:11:32,220
And it says, Please guess higher, so
you can see, is guess less than 5?

199
00:11:32,220 --> 00:11:33,190
Well yes, cuz it's 4.

200
00:11:33,190 --> 00:11:36,440
If guess is equal to 5,
print Well done, you guessed it.

201
00:11:36,440 --> 00:11:39,460
So if we type 5 in here now it should come
up and say, Well done, you guessed it.

202
00:11:39,460 --> 00:11:41,940
But if we run it again, and

203
00:11:41,940 --> 00:11:46,420
put four, please get's higher,
this time we're going to guess 890.

204
00:11:46,420 --> 00:11:49,740
Sorry you've not guess correctly.

205
00:11:49,740 --> 00:11:54,920
And lastly, we'll do a test and we'll say,
we'll start with ten, please guess lower.

206
00:11:54,920 --> 00:11:56,180
Your top five.

207
00:11:56,180 --> 00:11:58,560
Well now, you've guessed it.

208
00:11:58,560 --> 00:11:59,270
And run it again.

209
00:11:59,270 --> 00:12:01,810
12, please guess lower.

210
00:12:01,810 --> 00:12:04,560
11, sorry you've not guessed correctly.

211
00:12:04,560 --> 00:12:08,950
So, you can see, I hope you're now getting
an idea about how you can actually,

212
00:12:08,950 --> 00:12:12,970
how this code works, and how blocks can
be quite complex including further if and

213
00:12:12,970 --> 00:12:17,430
else blocks, and much more contained
within it, you saw how when testing for

214
00:12:17,430 --> 00:12:19,320
equality we don't use
a single equal symbol.

215
00:12:19,320 --> 00:12:23,380
We use the two symbols to actually check
to see whether value has a certain value.

216
00:12:23,380 --> 00:12:27,760
And of course a single equal is if you're
assigning a value as in this line here.

217
00:12:27,760 --> 00:12:32,150
Assigning the variable guess,
the value of whatever of was

218
00:12:32,150 --> 00:12:34,950
typed by the user converting
it automatically to a number.

219
00:12:34,950 --> 00:12:39,160
Now, and if statement can also include
many else ifs, elifs, and as you can see,

220
00:12:39,160 --> 00:12:39,960
I've already used one there.

221
00:12:39,960 --> 00:12:42,600
I've already used one elif,
and then at the last line,

222
00:12:42,600 --> 00:12:46,820
we used an else, which is sorta the final
test that we can actually have.

223
00:12:46,820 --> 00:12:49,440
If all else files,
it's gonna go to that part of the code.

224
00:12:49,440 --> 00:12:52,920
Now, there are much better ways of
writing code than what we've shown here,

225
00:12:52,920 --> 00:12:54,740
this is really just
an example to get you going.

226
00:12:54,740 --> 00:12:57,740
And you will see better
ways of doing this shortly.

227
00:12:57,740 --> 00:13:00,820
One of the bad things that we've done here
is we're actually duplicated code, so

228
00:13:00,820 --> 00:13:02,560
we've actually added this code here for

229
00:13:02,560 --> 00:13:07,250
example, we've duplicated there,
and we've duplicated it there.

230
00:13:07,250 --> 00:13:09,830
And we wouldn't want to do that in
a real-world scenario, but this again,

231
00:13:09,830 --> 00:13:11,230
just to get us going.

232
00:13:11,230 --> 00:13:13,380
If you ever see something like this
when you're programming though,

233
00:13:13,380 --> 00:13:16,510
that's probably a good indication
that the code that you're working on

234
00:13:16,510 --> 00:13:19,650
could be improved or
reducing that duplication.

235
00:13:19,650 --> 00:13:23,890
So, we saw that in a previous bit of code
this one up here, greater than or equal,

236
00:13:23,890 --> 00:13:26,360
you can actually test for
that using a greater than or

237
00:13:26,360 --> 00:13:27,790
equal sign as you saw there.

238
00:13:27,790 --> 00:13:30,500
We can test for
equality with the two equals.

239
00:13:30,500 --> 00:13:31,160
We can test for

240
00:13:31,160 --> 00:13:34,380
greater than which is the greater than
sign likewise with the less than.

241
00:13:34,380 --> 00:13:36,930
And there's a couple
of other ones as well.

242
00:13:36,930 --> 00:13:37,950
which we'll see as we go through.

243
00:13:37,950 --> 00:13:40,140
There's a not equal which
would be similar to that.

244
00:13:40,140 --> 00:13:41,220
Which would be like this.

245
00:13:41,220 --> 00:13:43,710
If you want to check something
that is not equal to.

246
00:13:43,710 --> 00:13:47,260
And there's also a less than and equal
which works just like a greater than does,

247
00:13:47,260 --> 00:13:49,660
but obviously,
it's the other side comparison,

248
00:13:49,660 --> 00:13:53,960
making sure our value is either
less than or equal to that value.

249
00:13:53,960 --> 00:13:56,780
So now,
we actually know how to actually check for

250
00:13:56,780 --> 00:14:00,590
nautical as I showed you briefly,
by doing something like that,

251
00:14:00,590 --> 00:14:02,960
we can actually change our code,
and make it a lot more efficient.

252
00:14:02,960 --> 00:14:04,390
So, what we could actually
do here is we could,

253
00:14:04,390 --> 00:14:08,950
instead of putting if guess is less than
5, we could put if guess not equal to 5.

254
00:14:08,950 --> 00:14:14,120
And the next time we could
put if guess less than 5,

255
00:14:14,120 --> 00:14:17,950
and then, we could indent that
to put please print higher.

256
00:14:17,950 --> 00:14:25,380
Else, print please guess lower.

257
00:14:27,310 --> 00:14:28,770
Please, guess lower.

258
00:14:30,480 --> 00:14:34,520
And what we can do then,
is we can put guess equals int, input.

259
00:14:37,170 --> 00:14:42,450
Then, put if guess, is equal to 5,

260
00:14:42,450 --> 00:14:47,390
print, well done you guessed it,

261
00:14:47,390 --> 00:14:51,140
else, print("Sorry,

262
00:14:51,140 --> 00:14:56,610
you have not guessed correctly").

263
00:14:58,950 --> 00:15:02,530
And then, removing this else if now,
the elif, which we no longer need.

264
00:15:02,530 --> 00:15:05,090
Deleting that, we can actually just
print("You got it first time").

265
00:15:05,090 --> 00:15:07,210
So, you can see we've now
consolidated that code.

266
00:15:07,210 --> 00:15:09,350
We're doing a test at the start.

267
00:15:09,350 --> 00:15:11,660
So, if guest is not equal to 5,

268
00:15:11,660 --> 00:15:16,790
so if any value other than 5 was entered,
we're gonna start processing down here.

269
00:15:16,790 --> 00:15:19,090
But if 5 was entered,
we're gonna go straight down to here,

270
00:15:19,090 --> 00:15:20,850
just how you got it first time.

271
00:15:20,850 --> 00:15:24,590
They were doing a test say if guess is
less than five, the message would say,

272
00:15:24,590 --> 00:15:27,550
please guess higher, because we
know the number needs to be higher.

273
00:15:27,550 --> 00:15:31,070
Else, at this point the number has to be
higher, so we print a message that says,

274
00:15:31,070 --> 00:15:32,560
please guess lower.

275
00:15:32,560 --> 00:15:36,380
And will ask for an input again, and
if the guess is correct, and equal to 5,

276
00:15:36,380 --> 00:15:39,600
we print a message that say, what we've
done you guessed it, should be guessed.

277
00:15:40,880 --> 00:15:43,930
Otherwise, we put in summary
you've not guessed correctly, and

278
00:15:43,930 --> 00:15:46,760
this funnel else,
is only called at this level

279
00:15:46,760 --> 00:15:49,090
which would be if they actually
got it right the first time.

280
00:15:49,090 --> 00:15:51,910
So, let's just run that, and
confirm that it still works.

281
00:15:51,910 --> 00:15:53,320
So, I enter number 5 first.

282
00:15:53,320 --> 00:15:54,600
He got it right, first time.

283
00:15:54,600 --> 00:15:58,870
So, the first test to see whether it's
not equal to 5 doesn't go into there, so

284
00:15:58,870 --> 00:16:01,180
therefore, it has to be equal
to 5 because we've tested for

285
00:16:01,180 --> 00:16:04,580
not equal here, so we're getting that
message correctly, gonna run this again,

286
00:16:04,580 --> 00:16:08,870
went to the number four,
please gets higher.

287
00:16:08,870 --> 00:16:09,840
Which you would expect it to do.

288
00:16:09,840 --> 00:16:13,350
Now, when I try another number this time
we'll say six, sorry it could not guess

289
00:16:13,350 --> 00:16:19,030
correctly, run it again four,
this time we'll actually guess five.

290
00:16:19,030 --> 00:16:22,390
Well, now you've guessed it, and that's
because this bit of code was executed

291
00:16:22,390 --> 00:16:25,690
because the second time we actually
asked for the input which was there.

292
00:16:25,690 --> 00:16:27,950
Just doing another test to see
whether we've got it right.

293
00:16:27,950 --> 00:16:31,290
And finally,
we'll hit a number like eight.

294
00:16:31,290 --> 00:16:32,560
Please guess lower.

295
00:16:32,560 --> 00:16:34,710
Five.
Well, now you've guessed it, and

296
00:16:34,710 --> 00:16:36,570
the last one we'll actually do a test for
eight.

297
00:16:38,280 --> 00:16:41,290
And so six,
sorry you have not guessed it correctly.

298
00:16:41,290 --> 00:16:44,230
So, you can see this really highlights
the importance of indenting your

299
00:16:44,230 --> 00:16:47,620
code in Python,
because the second guess, this one here,

300
00:16:47,620 --> 00:16:51,970
is not at all dependent on this other code
that's in this first block, so it's really

301
00:16:51,970 --> 00:16:56,010
important to structure your code, to put
your code in the right indentation level,

302
00:16:56,010 --> 00:17:00,290
and that helps you ensure that you're
executing the code in the right scenarios.

303
00:17:00,290 --> 00:17:04,410
So, for example, here that's only going
to execute is guests is less than five.

304
00:17:04,410 --> 00:17:07,460
This line's only going to be executed
if guests is greater than five,

305
00:17:07,460 --> 00:17:12,109
but in this case, here, this code is
going to be executed no matter what,

306
00:17:12,109 --> 00:17:14,720
because we haven't got any tests, and
it's not indented to the next level.

307
00:17:14,720 --> 00:17:18,119
Indented, it's going to be, running
the code at the same level, if you will.

308
00:17:18,119 --> 00:17:19,380
One of the other things
we could've done there.

309
00:17:19,380 --> 00:17:26,099
We could've done something like
# guest must be greater than 5.

310
00:17:26,099 --> 00:17:30,490
Which will tell us that, because in
this code here we can see that, okay,

311
00:17:30,490 --> 00:17:32,240
that's at less than 5 test.

312
00:17:32,240 --> 00:17:35,200
Sometimes with just an else it can
be a good idea to put a comment,

313
00:17:35,200 --> 00:17:38,970
just to spell out what the scenario is,
what this else is gonna be called for.

314
00:17:38,970 --> 00:17:42,420
In this case It's only going to be
executed if guess is greater than five, so

315
00:17:42,420 --> 00:17:44,270
that can be a good place to put a comment,

316
00:17:44,270 --> 00:17:46,190
just to confirm your
understanding of the actual code.

317
00:17:46,190 --> 00:17:50,420
And it also helps yourself certainly, but
also other programmers in the future,

318
00:17:50,420 --> 00:17:52,260
who are actually reading that
to figure out what's going on.

319
00:17:52,260 --> 00:17:56,420
And again, just a reminder that comments
are ignored by the Python interpreter, and

320
00:17:56,420 --> 00:17:58,560
they're there purely
to document your code.

321
00:17:58,560 --> 00:18:01,360
So generally, if you use comments to
explain why you've done something in

322
00:18:01,360 --> 00:18:05,630
a certain way, or as a reminder of
certain conditions, or variable states,

323
00:18:05,630 --> 00:18:08,360
that may not be obvious to someone
who's reading it for the first time.

324
00:18:08,360 --> 00:18:10,460
And remember that,
this is very important in comments,

325
00:18:10,460 --> 00:18:12,610
if you come back to tomorrow for
a code weeks or

326
00:18:12,610 --> 00:18:16,360
months after you wrote it, that
documentation can really come in handy.

327
00:18:16,360 --> 00:18:19,500
And just a final thing about comments
before we end this video is that

328
00:18:19,500 --> 00:18:22,010
Python does provide a way to
have multiline comments, and

329
00:18:22,010 --> 00:18:25,470
we'll be covering that in the section
on documentation, and doc strings, and

330
00:18:25,470 --> 00:18:26,970
you can find out more about it there.

331
00:18:26,970 --> 00:18:29,820
For now, if you want a comment to span
several lines just start each line

332
00:18:29,820 --> 00:18:30,730
with a hash.

333
00:18:30,730 --> 00:18:33,060
That you've seen at the top
of this code here now.

334
00:18:33,060 --> 00:18:34,800
Okay, so
we're gonna end this video here now.

335
00:18:34,800 --> 00:18:37,700
When we come back in the next video,
we're gonna start looking at

336
00:18:37,700 --> 00:18:40,960
more complicated conditions than the
simple ones that we've used so far, and

337
00:18:40,960 --> 00:18:45,470
show you how Python provides the means
to produce quite complex expressions.

