Now in the last lesson,
we saw how we could use if, elif, else to check for multiple conditions.
But in this case we're only checking one condition even though we have multiple,
because if this first condition is true,
then we would do whatever it is we need to do and then we would bypass
everything else.
Now what if you were in a situation where you need to check for multiple
conditions even if the previous one was already true?
Coming back to our rollercoaster ticketing problem,
if you're going on a good rollercoaster ride,
you'd probably want to keep a picture for the memories, right?
And our rollercoaster is no exception.
We want to be able to charge users an extra $3 if they want to purchase a ticket
that includes a photo. Now,
this is quite interesting because this is completely independent of their age or
their height.
Even if we've already gotten their age and height and determined their ticket
price, this is an extra question. Do you want a photo or not?
Yes or no. If you do,
then we're going to add $3 to your existing ticket price.
To do this, we would write multiple if conditions.
If condition 1 is true, then do a,
but then the code is going to go to the next case and check if condition 2 is
also true,
in which case it will do B and if the final condition is also true,
it's going to do C. Whereas on the example on the left
here, only one of these things, A, B, or C will be carried out.
Comparing the example on the left where we're using
if, elif, else, only one of these things A,
B or C will be carried out. But on the right hand side,
all three conditions are checked and if it's so happens that all three
conditions are true, then A,
B and C will all be executed.
Currently, our code operates like this. We check for their height.
If they're over 120 centimeters,
we check their age and depending on their age,
we give them a different price ticket.
What we now want is even after we've checked for their ticket price,
we want to ask them a question, do you want photos with your ticket?
And if the answer is yes, then we're going to add $3 to that bill,
no matter which type of ticket they've got.
And finally we give them the total bill. If they say no,
then we jump straight to the total bill and just tell them the price of their
ticket. So how do we implement this in our code?
Well, first let's change these print statements.
Instead of giving them the bill at each of these steps,
I'm going to tell them which type of ticket they're eligible for.
So the next thing to do is to ask them whether if they want a photo or not.
So I'm going to need to use an input, but the question is where do I put that?
Now the correct answer is that it has to be at the same indentation level as
this if block. So you can imagine this whole thing with the,
if elif and else as sort of belonging together.
Because they all relate to one thing:
what is their age and which type of ticket are they eligible for. Now,
outside of that if statement,
I'm going to create an input and ask the user,
do you want a photo taken? Type Y for yes or N for no.
And then I'm going to save their input inside a variable called wants_photo.
So now I'm going to use an if statement to check what their answer was.
If it was true, then I'm going to add $3 and if it was false,
I'm just going to skip to giving them their ticket price.
So here is where I write my if statement.
Notice how it's at the same indentation level as this previous set of if
statements.
But it's not at the same indentation level as this set of if statement.
So essentially what's happening is once I've checked their age,
then I'm going to check whether if they want a photo or not.
And this is going to apply to everybody no matter their age.
So if wants_photo is equal to Y,
well in this case I'm going to go and add $3 to their bill.
But how can I add $3 the bill when I don't have a variable to vary?
So instead of using these print statements,
I'm going to create a variable up here called bill and
I'm going to set it to equal zero.
Now in addition to printing to the user how much their ticket is,
I'm going to set the bill to the price that they're supposed to pay.
So if age is less than 12 bill is equal to $5, if age is less than 18 then bill
is equal to $7, and finally for everybody else,
the bill is equal to $12. So now depending on these conditions,
the variable bill is going to be changed to a different number.
But once we land in this if statement,
I'm going to have to add $3 to their bill
no matter which value it is at the moment.
So effectively, what I want to do is bill equals the current value of bill plus
$3. So if bill was $7,
then this new value bill should be $10. If bill was $12 then it should now be $15.
Now in Python as well as many other languages, there's actually a slightly shorter way
of writing this.
When you want to increase the current value that's held in a variable and you
wanna save it back into the variable,
you can simply write plus equals.
So bill += 3 is the same as bill = bill + 3.
Now, no matter what the value of bill is before it reached this if statement,
I'm still going to add $3 to it. Now after this
if statement is completed,
I don't actually have to write a companion else statement because in this case,
if the answer is no, then we're simply going to do nothing.
We're not going to do anything to the bill. Instead,
I'm just going to skip ahead and print to the user their final bill.
And I'm going to use fstrings to insert the value that the bill variable has
into this print statement. Now for your code to work,
the indentation matters a huge deal because the computer will think you want
it to do different things depending on the indentation.
So notice how this indentation shows that this bill plus three is to be executed
when this condition is true,
but this print statement is not indented to the same level and effectively it's
going to happen after this if statement has been executed.
So you can play around with indenting this print statements, un-indenting it
and see what the difference is.
But now I'm going to go ahead and hit command
+ enter or control + enter on Windows to run my code and I'm going to go ahead and
try and get a ticket.
So let's say that I am 21 years old,
tells me the adult tickets at $12 and then it asked me,
do you want a photo taken? So yeah, I would love to have a photo taken.
And it tells me that my final bill is 15,
so I forgot a dollar sign here.
But effectively it's now added the $3 to the $12 because of this additional if
statement. So if this concept is still a little bit confusing to you,
then I want you to really study this flow chart and just look at the difference,
um, and visualize it because the,
if elif else statement is only ever going to be true in one of these arrows,
right? You can't be less than 12 and 18 and over.
And similarly,
it doesn't make sense for you to have two different ticket prices.
But with the want photos,
this condition has to be asked no matter whichever branch they landed on.
So it's a completely separate from this if statement.
And once you've had a look at this flow chart,
then compare it against the code and the way that the code works should be a lot
more obvious. And once you're happy with the code and this concept,
then I want you to head over to the next lesson where I've got a code challenge
for you. So for all of that, and more, I'll see you there.