Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Now in the last lesson,
2
we saw how we could use if, elif, else to check for multiple conditions.
3
But in this case we're only checking one condition even though we have multiple,
4
because if this first condition is true,
5
then we would do whatever it is we need to do and then we would bypass
6
everything else.
7
Now what if you were in a situation where you need to check for multiple
8
conditions even if the previous one was already true?
9
Coming back to our rollercoaster ticketing problem,
10
if you're going on a good rollercoaster ride,
11
you'd probably want to keep a picture for the memories, right?
12
And our rollercoaster is no exception.
13
We want to be able to charge users an extra $3 if they want to purchase a ticket
14
that includes a photo. Now,
15
this is quite interesting because this is completely independent of their age or
16
their height.
17
Even if we've already gotten their age and height and determined their ticket
18
price, this is an extra question. Do you want a photo or not?
19
Yes or no. If you do,
20
then we're going to add $3 to your existing ticket price.
21
To do this, we would write multiple if conditions.
22
If condition 1 is true, then do a,
23
but then the code is going to go to the next case and check if condition 2 is
24
also true,
25
in which case it will do B and if the final condition is also true,
26
it's going to do C. Whereas on the example on the left
27
here, only one of these things, A, B, or C will be carried out.
28
Comparing the example on the left where we're using
29
if, elif, else, only one of these things A,
30
B or C will be carried out. But on the right hand side,
31
all three conditions are checked and if it's so happens that all three
32
conditions are true, then A,
33
B and C will all be executed.
34
Currently, our code operates like this. We check for their height.
35
If they're over 120 centimeters,
36
we check their age and depending on their age,
37
we give them a different price ticket.
38
What we now want is even after we've checked for their ticket price,
39
we want to ask them a question, do you want photos with your ticket?
40
And if the answer is yes, then we're going to add $3 to that bill,
41
no matter which type of ticket they've got.
42
And finally we give them the total bill. If they say no,
43
then we jump straight to the total bill and just tell them the price of their
44
ticket. So how do we implement this in our code?
45
Well, first let's change these print statements.
46
Instead of giving them the bill at each of these steps,
47
I'm going to tell them which type of ticket they're eligible for.
48
So the next thing to do is to ask them whether if they want a photo or not.
49
So I'm going to need to use an input, but the question is where do I put that?
50
Now the correct answer is that it has to be at the same indentation level as
51
this if block. So you can imagine this whole thing with the,
52
if elif and else as sort of belonging together.
53
Because they all relate to one thing:
54
what is their age and which type of ticket are they eligible for. Now,
55
outside of that if statement,
56
I'm going to create an input and ask the user,
57
do you want a photo taken? Type Y for yes or N for no.
58
And then I'm going to save their input inside a variable called wants_photo.
59
So now I'm going to use an if statement to check what their answer was.
60
If it was true, then I'm going to add $3 and if it was false,
61
I'm just going to skip to giving them their ticket price.
62
So here is where I write my if statement.
63
Notice how it's at the same indentation level as this previous set of if
64
statements.
65
But it's not at the same indentation level as this set of if statement.
66
So essentially what's happening is once I've checked their age,
67
then I'm going to check whether if they want a photo or not.
68
And this is going to apply to everybody no matter their age.
69
So if wants_photo is equal to Y,
70
well in this case I'm going to go and add $3 to their bill.
71
But how can I add $3 the bill when I don't have a variable to vary?
72
So instead of using these print statements,
73
I'm going to create a variable up here called bill and
74
I'm going to set it to equal zero.
75
Now in addition to printing to the user how much their ticket is,
76
I'm going to set the bill to the price that they're supposed to pay.
77
So if age is less than 12 bill is equal to $5, if age is less than 18 then bill
78
is equal to $7, and finally for everybody else,
79
the bill is equal to $12. So now depending on these conditions,
80
the variable bill is going to be changed to a different number.
81
But once we land in this if statement,
82
I'm going to have to add $3 to their bill
83
no matter which value it is at the moment.
84
So effectively, what I want to do is bill equals the current value of bill plus
85
$3. So if bill was $7,
86
then this new value bill should be $10. If bill was $12 then it should now be $15.
87
Now in Python as well as many other languages, there's actually a slightly shorter way
88
of writing this.
89
When you want to increase the current value that's held in a variable and you
90
wanna save it back into the variable,
91
you can simply write plus equals.
92
So bill += 3 is the same as bill = bill + 3.
93
Now, no matter what the value of bill is before it reached this if statement,
94
I'm still going to add $3 to it. Now after this
95
if statement is completed,
96
I don't actually have to write a companion else statement because in this case,
97
if the answer is no, then we're simply going to do nothing.
98
We're not going to do anything to the bill. Instead,
99
I'm just going to skip ahead and print to the user their final bill.
100
And I'm going to use fstrings to insert the value that the bill variable has
101
into this print statement. Now for your code to work,
102
the indentation matters a huge deal because the computer will think you want
103
it to do different things depending on the indentation.
104
So notice how this indentation shows that this bill plus three is to be executed
105
when this condition is true,
106
but this print statement is not indented to the same level and effectively it's
107
going to happen after this if statement has been executed.
108
So you can play around with indenting this print statements, un-indenting it
109
and see what the difference is.
110
But now I'm going to go ahead and hit command
111
+ enter or control + enter on Windows to run my code and I'm going to go ahead and
112
try and get a ticket.
113
So let's say that I am 21 years old,
114
tells me the adult tickets at $12 and then it asked me,
115
do you want a photo taken? So yeah, I would love to have a photo taken.
116
And it tells me that my final bill is 15,
117
so I forgot a dollar sign here.
118
But effectively it's now added the $3 to the $12 because of this additional if
119
statement. So if this concept is still a little bit confusing to you,
120
then I want you to really study this flow chart and just look at the difference,
121
um, and visualize it because the,
122
if elif else statement is only ever going to be true in one of these arrows,
123
right? You can't be less than 12 and 18 and over.
124
And similarly,
125
it doesn't make sense for you to have two different ticket prices.
126
But with the want photos,
127
this condition has to be asked no matter whichever branch they landed on.
128
So it's a completely separate from this if statement.
129
And once you've had a look at this flow chart,
130
then compare it against the code and the way that the code works should be a lot
131
more obvious. And once you're happy with the code and this concept,
132
then I want you to head over to the next lesson where I've got a code challenge
133
for you. So for all of that, and more, I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.