Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,100 --> 00:00:01,420
Now in the last lesson,
2
00:00:01,450 --> 00:00:06,450
we saw how we could use if, elif, else to check for multiple conditions.
3
00:00:06,970 --> 00:00:11,970
But in this case we're only checking one condition even though we have multiple,
4
00:00:12,460 --> 00:00:15,610
because if this first condition is true,
5
00:00:15,910 --> 00:00:20,350
then we would do whatever it is we need to do and then we would bypass
6
00:00:20,440 --> 00:00:21,460
everything else.
7
00:00:22,030 --> 00:00:25,810
Now what if you were in a situation where you need to check for multiple
8
00:00:25,810 --> 00:00:30,010
conditions even if the previous one was already true?
9
00:00:31,180 --> 00:00:33,910
Coming back to our rollercoaster ticketing problem,
10
00:00:34,420 --> 00:00:36,400
if you're going on a good rollercoaster ride,
11
00:00:36,550 --> 00:00:41,020
you'd probably want to keep a picture for the memories, right?
12
00:00:41,800 --> 00:00:44,020
And our rollercoaster is no exception.
13
00:00:44,470 --> 00:00:49,470
We want to be able to charge users an extra $3 if they want to purchase a ticket
14
00:00:49,690 --> 00:00:52,120
that includes a photo. Now,
15
00:00:52,150 --> 00:00:57,150
this is quite interesting because this is completely independent of their age or
16
00:00:57,910 --> 00:00:58,743
their height.
17
00:00:58,960 --> 00:01:02,500
Even if we've already gotten their age and height and determined their ticket
18
00:01:02,500 --> 00:01:07,450
price, this is an extra question. Do you want a photo or not?
19
00:01:07,480 --> 00:01:09,670
Yes or no. If you do,
20
00:01:09,700 --> 00:01:13,750
then we're going to add $3 to your existing ticket price.
21
00:01:14,650 --> 00:01:18,460
To do this, we would write multiple if conditions.
22
00:01:19,030 --> 00:01:21,940
If condition 1 is true, then do a,
23
00:01:22,360 --> 00:01:27,070
but then the code is going to go to the next case and check if condition 2 is
24
00:01:27,070 --> 00:01:27,903
also true,
25
00:01:28,180 --> 00:01:32,320
in which case it will do B and if the final condition is also true,
26
00:01:32,440 --> 00:01:35,680
it's going to do C. Whereas on the example on the left
27
00:01:35,680 --> 00:01:40,270
here, only one of these things, A, B, or C will be carried out.
28
00:01:40,690 --> 00:01:44,230
Comparing the example on the left where we're using
29
00:01:44,260 --> 00:01:48,370
if, elif, else, only one of these things A,
30
00:01:48,370 --> 00:01:52,480
B or C will be carried out. But on the right hand side,
31
00:01:52,900 --> 00:01:57,070
all three conditions are checked and if it's so happens that all three
32
00:01:57,070 --> 00:01:59,470
conditions are true, then A,
33
00:01:59,590 --> 00:02:02,860
B and C will all be executed.
34
00:02:03,520 --> 00:02:08,320
Currently, our code operates like this. We check for their height.
35
00:02:08,950 --> 00:02:11,020
If they're over 120 centimeters,
36
00:02:11,350 --> 00:02:13,990
we check their age and depending on their age,
37
00:02:14,380 --> 00:02:16,990
we give them a different price ticket.
38
00:02:18,130 --> 00:02:23,130
What we now want is even after we've checked for their ticket price,
39
00:02:24,040 --> 00:02:28,510
we want to ask them a question, do you want photos with your ticket?
40
00:02:29,200 --> 00:02:32,800
And if the answer is yes, then we're going to add $3 to that bill,
41
00:02:33,340 --> 00:02:35,500
no matter which type of ticket they've got.
42
00:02:36,220 --> 00:02:39,490
And finally we give them the total bill. If they say no,
43
00:02:39,550 --> 00:02:43,090
then we jump straight to the total bill and just tell them the price of their
44
00:02:43,090 --> 00:02:47,230
ticket. So how do we implement this in our code?
45
00:02:48,970 --> 00:02:52,240
Well, first let's change these print statements.
46
00:02:52,270 --> 00:02:55,420
Instead of giving them the bill at each of these steps,
47
00:02:55,750 --> 00:02:58,930
I'm going to tell them which type of ticket they're eligible for.
48
00:03:02,560 --> 00:03:07,450
So the next thing to do is to ask them whether if they want a photo or not.
49
00:03:07,870 --> 00:03:12,730
So I'm going to need to use an input, but the question is where do I put that?
50
00:03:13,450 --> 00:03:18,450
Now the correct answer is that it has to be at the same indentation level as
51
00:03:20,080 --> 00:03:23,860
this if block. So you can imagine this whole thing with the,
52
00:03:23,860 --> 00:03:27,640
if elif and else as sort of belonging together.
53
00:03:28,420 --> 00:03:30,400
Because they all relate to one thing:
54
00:03:30,760 --> 00:03:35,590
what is their age and which type of ticket are they eligible for. Now,
55
00:03:35,620 --> 00:03:37,540
outside of that if statement,
56
00:03:37,780 --> 00:03:41,650
I'm going to create an input and ask the user,
57
00:03:42,370 --> 00:03:46,360
do you want a photo taken? Type Y for yes or N for no.
58
00:03:47,110 --> 00:03:52,110
And then I'm going to save their input inside a variable called wants_photo.
59
00:03:55,120 --> 00:04:00,120
So now I'm going to use an if statement to check what their answer was.
60
00:04:00,700 --> 00:04:04,240
If it was true, then I'm going to add $3 and if it was false,
61
00:04:04,300 --> 00:04:07,120
I'm just going to skip to giving them their ticket price.
62
00:04:07,840 --> 00:04:10,810
So here is where I write my if statement.
63
00:04:11,440 --> 00:04:16,440
Notice how it's at the same indentation level as this previous set of if
64
00:04:16,630 --> 00:04:17,463
statements.
65
00:04:17,920 --> 00:04:22,920
But it's not at the same indentation level as this set of if statement.
66
00:04:24,220 --> 00:04:27,760
So essentially what's happening is once I've checked their age,
67
00:04:28,270 --> 00:04:31,720
then I'm going to check whether if they want a photo or not.
68
00:04:32,050 --> 00:04:35,710
And this is going to apply to everybody no matter their age.
69
00:04:36,490 --> 00:04:40,930
So if wants_photo is equal to Y,
70
00:04:41,680 --> 00:04:46,680
well in this case I'm going to go and add $3 to their bill.
71
00:04:48,970 --> 00:04:53,970
But how can I add $3 the bill when I don't have a variable to vary?
72
00:04:55,480 --> 00:04:57,730
So instead of using these print statements,
73
00:04:58,450 --> 00:05:01,120
I'm going to create a variable up here called bill and
74
00:05:01,460 --> 00:05:03,400
I'm going to set it to equal zero.
75
00:05:04,600 --> 00:05:09,010
Now in addition to printing to the user how much their ticket is,
76
00:05:09,340 --> 00:05:13,450
I'm going to set the bill to the price that they're supposed to pay.
77
00:05:13,930 --> 00:05:18,930
So if age is less than 12 bill is equal to $5, if age is less than 18 then bill
78
00:05:19,750 --> 00:05:23,170
is equal to $7, and finally for everybody else,
79
00:05:23,200 --> 00:05:28,200
the bill is equal to $12. So now depending on these conditions,
80
00:05:30,460 --> 00:05:34,780
the variable bill is going to be changed to a different number.
81
00:05:35,470 --> 00:05:38,260
But once we land in this if statement,
82
00:05:38,560 --> 00:05:42,400
I'm going to have to add $3 to their bill
83
00:05:42,700 --> 00:05:45,340
no matter which value it is at the moment.
84
00:05:45,880 --> 00:05:50,880
So effectively, what I want to do is bill equals the current value of bill plus
85
00:05:51,700 --> 00:05:54,130
$3. So if bill was $7,
86
00:05:54,160 --> 00:05:59,160
then this new value bill should be $10. If bill was $12 then it should now be $15.
87
00:06:00,530 --> 00:06:05,240
Now in Python as well as many other languages, there's actually a slightly shorter way
88
00:06:05,240 --> 00:06:06,260
of writing this.
89
00:06:06,740 --> 00:06:11,690
When you want to increase the current value that's held in a variable and you
90
00:06:11,690 --> 00:06:13,610
wanna save it back into the variable,
91
00:06:13,970 --> 00:06:17,240
you can simply write plus equals.
92
00:06:17,810 --> 00:06:22,040
So bill += 3 is the same as bill = bill + 3.
93
00:06:22,790 --> 00:06:27,680
Now, no matter what the value of bill is before it reached this if statement,
94
00:06:28,040 --> 00:06:32,930
I'm still going to add $3 to it. Now after this
95
00:06:32,930 --> 00:06:34,580
if statement is completed,
96
00:06:34,880 --> 00:06:39,880
I don't actually have to write a companion else statement because in this case,
97
00:06:40,940 --> 00:06:45,080
if the answer is no, then we're simply going to do nothing.
98
00:06:45,080 --> 00:06:48,380
We're not going to do anything to the bill. Instead,
99
00:06:48,410 --> 00:06:52,610
I'm just going to skip ahead and print to the user their final bill.
100
00:06:55,370 --> 00:07:00,370
And I'm going to use fstrings to insert the value that the bill variable has
101
00:07:02,990 --> 00:07:07,820
into this print statement. Now for your code to work,
102
00:07:08,000 --> 00:07:13,000
the indentation matters a huge deal because the computer will think you want
103
00:07:14,750 --> 00:07:18,320
it to do different things depending on the indentation.
104
00:07:19,040 --> 00:07:24,040
So notice how this indentation shows that this bill plus three is to be executed
105
00:07:26,510 --> 00:07:27,890
when this condition is true,
106
00:07:28,580 --> 00:07:33,580
but this print statement is not indented to the same level and effectively it's
107
00:07:34,220 --> 00:07:38,300
going to happen after this if statement has been executed.
108
00:07:39,320 --> 00:07:44,320
So you can play around with indenting this print statements, un-indenting it
109
00:07:45,230 --> 00:07:46,730
and see what the difference is.
110
00:07:47,810 --> 00:07:51,770
But now I'm going to go ahead and hit command
111
00:07:51,800 --> 00:07:56,800
+ enter or control + enter on Windows to run my code and I'm going to go ahead and
112
00:07:58,070 --> 00:07:59,330
try and get a ticket.
113
00:08:00,050 --> 00:08:03,530
So let's say that I am 21 years old,
114
00:08:04,730 --> 00:08:08,150
tells me the adult tickets at $12 and then it asked me,
115
00:08:08,150 --> 00:08:11,810
do you want a photo taken? So yeah, I would love to have a photo taken.
116
00:08:12,560 --> 00:08:15,530
And it tells me that my final bill is 15,
117
00:08:16,010 --> 00:08:18,170
so I forgot a dollar sign here.
118
00:08:18,710 --> 00:08:23,710
But effectively it's now added the $3 to the $12 because of this additional if
119
00:08:26,510 --> 00:08:31,460
statement. So if this concept is still a little bit confusing to you,
120
00:08:31,880 --> 00:08:36,880
then I want you to really study this flow chart and just look at the difference,
121
00:08:37,460 --> 00:08:40,550
um, and visualize it because the,
122
00:08:40,670 --> 00:08:45,670
if elif else statement is only ever going to be true in one of these arrows,
123
00:08:47,450 --> 00:08:51,410
right? You can't be less than 12 and 18 and over.
124
00:08:52,280 --> 00:08:53,030
And similarly,
125
00:08:53,030 --> 00:08:56,460
it doesn't make sense for you to have two different ticket prices.
126
00:08:57,270 --> 00:08:59,250
But with the want photos,
127
00:08:59,550 --> 00:09:04,550
this condition has to be asked no matter whichever branch they landed on.
128
00:09:05,160 --> 00:09:08,130
So it's a completely separate from this if statement.
129
00:09:09,030 --> 00:09:11,910
And once you've had a look at this flow chart,
130
00:09:12,120 --> 00:09:16,980
then compare it against the code and the way that the code works should be a lot
131
00:09:16,980 --> 00:09:21,360
more obvious. And once you're happy with the code and this concept,
132
00:09:21,690 --> 00:09:25,440
then I want you to head over to the next lesson where I've got a code challenge
133
00:09:25,440 --> 00:09:29,340
for you. So for all of that, and more, I'll see you there.
13020
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.