WEBVTT

0
00:00.080 --> 00:07.160
Now in the last lesson, we saw how we could use if / elif /else to check for multiple conditions, but

1
00:07.160 --> 00:12.200
in this case we're only checking one condition, even though we have multiple.

2
00:12.200 --> 00:19.460
Because if this first condition is true, then we would do whatever it is we need to do and then we

3
00:19.460 --> 00:21.650
would bypass everything else.

4
00:21.680 --> 00:28.820
Now what if you're in a situation where you need to check for multiple conditions, even if the previous

5
00:28.820 --> 00:30.210
one was already true.

6
00:30.900 --> 00:36.360
Coming back to our roller coaster ticketing problem, if you're going on a good roller coaster ride,

7
00:36.360 --> 00:41.220
you'd probably want to keep a picture for the memories, right?

8
00:41.550 --> 00:44.190
And our roller coaster is no exception.

9
00:44.190 --> 00:51.330
We want to be able to charge users an extra $3 if they want to purchase a ticket that includes a photo.

10
00:51.630 --> 00:58.820
Now, this is quite interesting because this is completely independent of their age or their height,

11
00:58.820 --> 01:03.140
even if we've already gotten their age and height and determined their ticket price.

12
01:03.140 --> 01:05.360
This is an extra question.

13
01:05.360 --> 01:07.460
Do you want a photo or not?

14
01:07.460 --> 01:08.840
Yes or no?

15
01:08.840 --> 01:13.910
If you do, then we're going to add $3 to your existing ticket price.

16
01:14.570 --> 01:18.730
To do this we would write multiple if conditions.

17
01:18.760 --> 01:22.060
If condition1 is true, then do A,

18
01:22.060 --> 01:28.450
but then the code is going to go to the next case and check if condition2 is also true, in which

19
01:28.450 --> 01:35.110
case it will do B, and if the final condition is also true, it's going to do C. Whereas on the example

20
01:35.110 --> 01:42.380
on the left here, only one of these things, A, B or C will be carried out. Comparing the example on

21
01:42.380 --> 01:44.270
the left where we're using,

22
01:44.270 --> 01:50.330
If / elif / else only one of these things, A, B, or C will be carried out.

23
01:50.540 --> 01:54.860
But on the right-hand side, all three conditions are checked,

24
01:54.860 --> 02:03.020
and if it so happens that all three conditions are true, then A, B, and C will all be executed.

25
02:03.440 --> 02:06.780
Currently our code operates like this.

26
02:06.780 --> 02:08.520
We check for their height,

27
02:08.640 --> 02:16.530
if they are over 120cm, we check their age, and depending on their age, we give them a different price

28
02:16.530 --> 02:17.220
ticket.

29
02:17.880 --> 02:26.190
What we now want is even after we've checked for their ticket price, we want to ask them a question.

30
02:26.190 --> 02:28.740
Do you want photos with your ticket?

31
02:28.830 --> 02:34.840
And if the answer is yes, then we're going to add $3 to their bill, no matter which type of ticket

32
02:34.840 --> 02:35.770
they've got.

33
02:35.800 --> 02:38.290
And finally we give them the total bill.

34
02:38.290 --> 02:43.600
If they say no, then we jump straight to the total bill and just tell them the price of their ticket.

35
02:44.080 --> 02:47.410
So how do we implement this in our code?

36
02:48.790 --> 02:55.550
Well, first let's change these print statements instead of giving them the bill at each of these steps,

37
02:55.550 --> 02:59.150
I'm going to tell them which type of ticket they're eligible for.

38
03:02.300 --> 03:07.610
So the next thing to do is to ask them whether if they want a photo or not.

39
03:07.610 --> 03:09.950
So I'm going to need to use an input,

40
03:09.950 --> 03:12.980
but the question is where do I put that?

41
03:13.130 --> 03:21.450
Now the correct answer is that it has to be at the same indentation level as this if block.

42
03:21.450 --> 03:28.740
So you can imagine this whole thing with the if, elif, and else as sort of belonging together, because

43
03:28.740 --> 03:30.570
they all relate to one thing.

44
03:30.570 --> 03:34.950
What is their age, and which type of ticket are they eligible for?

45
03:34.950 --> 03:43.230
Now, outside of that if statement, I'm going to create an input and ask the user, "Do you want a photo

46
03:43.230 --> 03:43.980
taken?"

47
03:44.010 --> 03:46.710
Type y for yes or n for no?

48
03:46.710 --> 03:52.470
And then I'm going to save that input inside a variable called wants_photo.

49
03:54.840 --> 04:00.390
So now I'm going to use an if statement to check what their answer was.

50
04:00.390 --> 04:03.210
If it was true then I'm going to add $3,

51
04:03.210 --> 04:07.380
and if it was false I'm just going to skip to giving them their ticket price.

52
04:07.510 --> 04:11.050
So here is where I write my if statement.

53
04:11.170 --> 04:19.180
Notice how it's at the same indentation level as this previous set of if statements, but it's not at

54
04:19.180 --> 04:23.890
the same indentation level as this set of if statement.

55
04:23.890 --> 04:30.340
So essentially what's happening is once I've checked their age, then I'm going to check whether if

56
04:30.340 --> 04:31.790
they want a photo or not.

57
04:31.790 --> 04:35.870
And this is going to apply to everybody, no matter their age.

58
04:36.170 --> 04:41.060
So if wants_photo is equal to y,

59
04:41.330 --> 04:47.870
well, in this case I'm going to go and add $3 to their bill.

60
04:48.650 --> 04:55.010
But how can I add $3 to the bill when I don't have a variable to vary.

61
04:55.160 --> 05:01.170
So instead of using these print statements, I'm going to create a variable up here called bill,

62
05:01.170 --> 05:03.420
and I'm going to set it to equal zero.

63
05:04.290 --> 05:11.880
Now in addition to printing to the user how much their ticket is, I'm going to set the bill to the

64
05:11.880 --> 05:13.590
price that they're supposed to pay.

65
05:13.620 --> 05:17.340
So if age is less than 12, bill is equal to five.

66
05:17.340 --> 05:21.010
If age is less than 18, then bill is equal to seven.

67
05:21.010 --> 05:25.930
And finally, for everybody else the bill is equal to 12.

68
05:25.960 --> 05:35.050
So now depending on these conditions, the variable bill is going to be changed to a different number.

69
05:35.140 --> 05:44.140
But once we land in this if statement I'm going to have to add $3 to their bill, no matter which value

70
05:44.140 --> 05:45.560
it is at the moment.

71
05:45.560 --> 05:52.400
So effectively, what I want to do is bill equals the current value of bill plus three.

72
05:52.400 --> 05:57.200
So if bill was seven, then this new value of bill should be ten.

73
05:57.200 --> 06:00.080
If bill was 12 then it should now be 15.

74
06:00.200 --> 06:03.380
Now in Python as well as many other languages.

75
06:03.380 --> 06:09.710
There's actually a slightly shorter way of writing this when you want to increase the current value

76
06:09.710 --> 06:16.580
that's held in a variable and you want to save it back into the variable, you can simply write plus

77
06:16.580 --> 06:17.480
equals (+=).

78
06:17.480 --> 06:22.310
So bill +=3 is the same as bill = bill + 3.

79
06:22.520 --> 06:30.110
Now no matter what the value of the bill is before it reaches this if statement I'm still going to add $3

80
06:30.110 --> 06:30.620
to it.

81
06:31.250 --> 06:39.030
Now after this if statement is completed, I don't actually have to write a companion else statement

82
06:39.030 --> 06:45.030
because in this case, if the answer is no, then we're simply going to do nothing.

83
06:45.030 --> 06:47.100
We're not going to do anything to the bill.

84
06:47.430 --> 06:52.830
Instead, I'm just going to skip ahead and print the user their final bill.

85
06:54.990 --> 07:04.630
And I'm going to use f-strings to insert the value that the bill variable has into this print statement.

86
07:06.010 --> 07:07.900
Now for your code to work,

87
07:07.900 --> 07:16.000
the indentation matters a huge deal because the computer will think you want it to do different things

88
07:16.000 --> 07:18.610
depending on the indentation.

89
07:18.700 --> 07:27.380
So notice how this indentation shows that this bill + 3 is to be executed when this condition

90
07:27.380 --> 07:28.220
is true.

91
07:28.250 --> 07:33.050
But this print statement is not indented to the same level,

92
07:33.050 --> 07:38.420
and effectively it's going to happen after this if statement has been executed.

93
07:39.020 --> 07:46.290
So you can play around with indenting this print statement and indenting it and see what the difference

94
07:46.290 --> 07:46.950
is.

95
07:47.610 --> 07:53.880
But now I'm going to go ahead and run my code and try and get a ticket.

96
07:54.120 --> 08:01.410
So let's say that I am 21 years old, it tells me that, "Adult tickets are $12."

97
08:01.410 --> 08:03.570
And then it ask me, "Do you want a photo taken?"

98
08:03.570 --> 08:06.480
So yeah, I would love to have a photo taken.

99
08:06.480 --> 08:12.670
And it tells me that my final bill is 15, so I've got a dollar sign here.

100
08:12.670 --> 08:21.580
But effectively it's now added the $3 to the $12 because of this additional if statement.

101
08:22.450 --> 08:29.740
So if this concept is still a little bit confusing to you, then I want you to really study this flowchart

102
08:29.740 --> 08:33.340
and just look at the difference, and visualize it,

103
08:33.340 --> 08:41.710
because the if / elif / else statement is only ever going to be true in one of these arrows.

104
08:41.710 --> 08:42.130
Right?

105
08:42.130 --> 08:45.970
You can't be less than 12 and 18 and over.

106
08:46.270 --> 08:51.010
And similarly, it doesn't make sense for you to have two different ticket prices.

107
08:51.220 --> 08:59.390
But with the want photos, this condition has to be asked no matter which ever branch they landed on.

108
08:59.390 --> 09:02.690
So it's completely separate from this if statement.

109
09:02.990 --> 09:08.630
And once you've had a look at this flowchart, then compare it against the code.

110
09:08.630 --> 09:12.350
And the way that the code works should be a lot more obvious.

111
09:12.590 --> 09:18.290
And once you're happy with the code and this concept, then I want you to head over to the next lesson

112
09:18.290 --> 09:20.270
where I've got a code challenge for you.

113
09:20.840 --> 09:23.600
So for all of that and more, I'll see you there.