WEBVTT

0
00:00.410 --> 00:07.250
Here's a question, have you ever sat in your bath and wondered, why is it that no matter how forgetful

1
00:07.250 --> 00:10.550
I am, my bath never overflows like this?

2
00:10.550 --> 00:15.710
This is something I was thinking about the other day, and I realized that it's not because I'm particularly

3
00:15.710 --> 00:16.280
diligent,

4
00:16.280 --> 00:18.620
I forget stuff all the time.

5
00:18.620 --> 00:20.480
I mean pizza, anyone?

6
00:21.290 --> 00:23.570
It's what my pizzas usually look like.

7
00:23.750 --> 00:31.960
But the reason why our bathtub or the sink doesn't overflow is because of this fantastic piece

8
00:31.960 --> 00:34.450
of engineering, the overflow.

9
00:34.480 --> 00:43.180
So this means that whenever the water reaches beyond a certain level about here, the water starts overflowing

10
00:43.180 --> 00:47.830
so that the bathtub doesn't overflow and annoy your neighbors downstairs.

11
00:48.280 --> 00:53.350
In fact, we could represent this mechanism with a conditional statement.

12
00:53.410 --> 01:00.790
When the water level is, say, greater than 80cm, then it should drain the water.

13
01:00.790 --> 01:07.480
But if the water level is not greater than 80cm, it should continue filling up the tub.

14
01:07.510 --> 01:12.790
This type of conditional statement is known as an if / else statement.

15
01:12.790 --> 01:21.290
Depending on a particular condition, we would do either A or B, and when we want to write Python code

16
01:21.290 --> 01:24.050
to represent this, it looks something like this.

17
01:24.050 --> 01:29.180
There's the keyword, if, and then the condition that we're testing for,

18
01:29.180 --> 01:31.100
and then a colon.

19
01:31.100 --> 01:38.270
And after the colon we've got an indented block of code which should be executed if this condition is

20
01:38.270 --> 01:38.570
met,

21
01:38.570 --> 01:46.180
if it's true, but if it's not true, then we will skip to the else block and it's just the else keyword

22
01:46.180 --> 01:47.410
with a colon.

23
01:47.410 --> 01:52.330
And then this code block would execute if the condition is false.

24
01:52.540 --> 01:58.060
So we could represent that previous bathtub situation with code that looks a bit like this.

25
01:58.060 --> 02:00.940
Let's say our water level is at 50cm,

26
02:00.940 --> 02:06.010
and then we would test if the water level is greater than 80cm.

27
02:06.010 --> 02:10.060
Well, if that is the case, then we should drain the water,

28
02:10.060 --> 02:13.060
but if it's not greater than 80cm,

29
02:13.060 --> 02:14.440
in other words, else.

30
02:14.440 --> 02:18.040
Well, in this case, we should just continue filling up the bathtub.

31
02:18.040 --> 02:21.670
Let's put this into practice with a real life problem.

32
02:21.670 --> 02:28.210
Now, let's say that you've gotten a job at a theme park, and your first job of the day is to write

33
02:28.210 --> 02:31.490
some code that replaces the ticket box.

34
02:31.700 --> 02:34.700
Now there's a couple of things that you'll need to think about.

35
02:34.700 --> 02:41.180
Firstly, in order for somebody to actually purchase a ticket to go on the roller coaster ride, they

36
02:41.180 --> 02:44.240
will need to be over 120cm.

37
02:44.330 --> 02:50.240
So we have to check what their height is, because if they're too short, then we won't be able to sell

38
02:50.240 --> 02:51.740
them a ticket anyways.

39
02:52.040 --> 02:59.560
In the Course resources I've included a link to this flow chart that I've created on Draw.io.

40
02:59.590 --> 03:05.290
This is a really, really useful tool for creating any sort of flow charts or diagrams, and it's really

41
03:05.290 --> 03:06.430
easy to use.

42
03:06.610 --> 03:13.990
Now, if we take a look at this flow chart, this is basically the logic that we have to program using

43
03:13.990 --> 03:16.030
our if and else statements.

44
03:16.060 --> 03:25.440
If the person who is trying to purchase a ticket is not over 120cm, then they can't ride on the roller

45
03:25.440 --> 03:26.220
coaster,

46
03:26.220 --> 03:30.690
but if their height is greater than 120cm, then they can ride.

47
03:30.690 --> 03:32.460
So let's try it out.

48
03:32.460 --> 03:38.820
All that I've got here is a print statement that says, "Welcome to the roller coaster!" as well as an

49
03:38.820 --> 03:46.430
input asking the user for their height in centimeters and then converting the string into a whole number,

50
03:46.430 --> 03:50.750
an integer, and then I'm storing it inside this variable called height.

51
03:50.960 --> 03:57.560
Now we're going to check whether if the height that the user is typed in is greater than 120.

52
03:57.560 --> 04:04.430
So we use the keyword if and then we check if the height is greater than 120,

53
04:04.430 --> 04:06.560
and then we add the colon.

54
04:06.680 --> 04:13.930
And now when I hit Enter, you'll notice that the code editor has automatically indented me slightly

55
04:13.930 --> 04:14.530
over.

56
04:14.530 --> 04:22.390
I'm not over here, because in Python the spacing and indentation is really, really important.

57
04:22.390 --> 04:29.650
It tells the computer that the code that I'm about to write is what should be executed when this condition

58
04:29.650 --> 04:30.670
is met.

59
04:30.790 --> 04:34.650
So what should happen if your height is over 120?

60
04:34.680 --> 04:36.570
Then we will just print,

61
04:36.570 --> 04:38.820
"You can ride the roller coaster."

62
04:38.970 --> 04:44.070
But what should we print if height is not greater than 120?

63
04:44.220 --> 04:50.250
Well, in this case we would use the else statement to catch when that happens.

64
04:50.250 --> 04:57.410
And it's really important that you don't write the else here because this is indented over.

65
04:57.560 --> 05:03.380
Instead, you want it to be at the same indentation level as the if statement.

66
05:03.410 --> 05:06.680
These two are essentially a pair if and else.

67
05:06.770 --> 05:12.170
Now after the else keyword, we again add a colon, and then we hit enter.

68
05:12.170 --> 05:13.640
And we're now indented again.

69
05:13.640 --> 05:19.790
And here we can write the code that should happen if this condition is false.

70
05:20.410 --> 05:21.010
"Sorry,

71
05:21.010 --> 05:22.870
you have to grow taller before you can ride."

72
05:23.410 --> 05:31.510
So the really important things here are the condition which we're testing for, is the value of height

73
05:31.510 --> 05:33.610
greater than 120.

74
05:33.790 --> 05:36.130
The syntax of this code.

75
05:36.130 --> 05:42.850
So the keywords if and else, as well as the colons that come after each of these lines.

76
05:43.240 --> 05:46.360
And finally also the indentation.

77
05:46.420 --> 05:52.420
Everything that is indented after the if is a block of code.

78
05:52.510 --> 05:59.920
So this is indented and it's effectively inside this if.

79
06:00.040 --> 06:04.630
So this line of code lives inside the else statement.

80
06:04.630 --> 06:08.620
This block of code lives inside the if statement.

81
06:09.030 --> 06:15.630
and if you mess up on the indentation, then you're probably going to get an IndentationError telling

82
06:15.630 --> 06:20.310
you that this line five probably should be indented.

83
06:20.310 --> 06:28.140
But when we do actually correct it and we hit Run, let's say that our height is 130cm,

84
06:28.140 --> 06:29.580
then we get back,

85
06:29.580 --> 06:38.000
"You can ride the roller coaster." But if our height was, say, 90cm, then we get a different outcome.

86
06:38.000 --> 06:40.910
We get, "Sorry you have to grow taller before you can ride."

87
06:41.000 --> 06:48.140
So by using if and else statements, we're able to get our code to do different things, either printing

88
06:48.140 --> 06:54.080
this line or printing this line depending on a condition that we're testing for.

89
06:54.230 --> 07:01.960
Now, when we use this greater than sign (&gt;) effectively, what we're saying is that is the height greater

90
07:01.960 --> 07:07.840
than 120, which means that it does not include 120.

91
07:07.990 --> 07:15.520
In fact, if I run this code and if I type in my height as 120, then it actually goes into the else

92
07:15.520 --> 07:17.710
block and prints this.

93
07:17.920 --> 07:27.690
So if we wanted to include 120cm so that all the people who are exactly 120cm can ride the roller coaster,

94
07:27.690 --> 07:33.240
then instead of just using a greater than symbol, we have to write greater than or equals (&gt;=).

95
07:33.240 --> 07:36.390
So these two symbols have to be next to each other.

96
07:37.020 --> 07:44.790
And now when I run my code and I write 120, you'll see that it's now falling into this block of code.

97
07:44.790 --> 07:47.360
And it's telling me that I can ride the roller coaster.

98
07:47.900 --> 07:52.940
Now, these are called comparison operators, and we've already seen Greater Than.

99
07:52.940 --> 07:59.810
So Lesser than is pretty self-explanatory, but we've also seen greater than or equal to and lesser

100
07:59.810 --> 08:00.980
than or equal to.

101
08:01.010 --> 08:07.400
So if you want to include a particular number or particular value when you do these comparisons, you

102
08:07.400 --> 08:09.320
would be using these instead.

103
08:09.470 --> 08:14.570
Now in this table there's also the equal to and not equal to.

104
08:14.990 --> 08:22.820
For example, if you wanted to check if somebody's height is equal to precisely 120, then you would

105
08:22.820 --> 08:25.130
use two equal signs.

106
08:25.130 --> 08:30.770
And very often it gets a little bit confusing, especially if you're new to programming when you're

107
08:30.770 --> 08:32.120
typing equal signs.

108
08:32.120 --> 08:36.030
Because sometimes we're typing one and other times we're typing two.

109
08:36.060 --> 08:42.540
It's important to remember that when you have one equal sign, it means that you're assigning this value

110
08:42.540 --> 08:44.460
to this variable.

111
08:44.460 --> 08:50.610
But when you have two equal signs, you're checking to see if the value on the left is equal to the

112
08:50.610 --> 08:52.020
value on the right.

113
08:52.020 --> 08:53.850
And they're completely different.

114
08:53.880 --> 09:00.440
Now, the good thing is that when you get it wrong, usually you'll get enough clues in the error to

115
09:00.440 --> 09:05.150
actually hint to you, hey, maybe there's something that's wrong here, right?

116
09:05.150 --> 09:07.280
Because the syntax doesn't look right.

117
09:07.280 --> 09:12.680
For this to be a condition, it has to be something that evaluates to true or to false.

118
09:12.680 --> 09:16.490
And height equals 120 does not evaluate to either.

119
09:16.490 --> 09:23.860
So by changing it to this we're saying if height is equal to 120 then we'll execute this line of code

120
09:23.920 --> 09:27.100
or any other lines which are in the same block of code.

121
09:27.100 --> 09:33.340
But if the height is not equal to 120, then we're going to execute this block of code.

122
09:33.370 --> 09:36.850
So if I write 120 then that works.

123
09:36.850 --> 09:42.040
But if I write 121 then it does not work.

124
09:42.490 --> 09:47.710
Similarly, you can also check for not equals to which just flips it around.

125
09:47.710 --> 09:56.290
But in our case it makes sense to say if the height is greater than or equals to 120, then you can

126
09:56.290 --> 10:04.330
ride the roller coaster, but otherwise, you cannot ride the roller coaster. By using if and else statements,

127
10:04.330 --> 10:11.530
we can get our computer to do different things and respond differently depending on different conditions.

128
10:11.530 --> 10:15.200
So in the next lesson, I've got a coding exercise for you,  

129
10:15.230 --> 10:16.250
I'll see you there.