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're in a situation where you need to check for multiple conditions, even if the previous
one was already true.
Coming back to our roller coaster ticketing problem, if you're going on a good roller coaster ride,
you'd probably want to keep a picture for the memories, right?
And our roller coaster 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 condition1 is true, then do A,
but then the code is going to go to the next case and check if condition2 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 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 are over 120cm, 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 their 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 that 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 to 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 five.
If age is less than 18, then bill is equal to seven.
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 three.
So if bill was seven, then this new value of bill should be ten.
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 want to 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 the bill is before it reaches 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 the user their final bill.
And I'm going to use f-strings 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 + 3 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 statement and indenting it and see what the difference
is.
But now I'm going to go ahead and run my code and try and get a ticket.
So let's say that I am 21 years old, it tells me that, "Adult tickets are $12."
And then it ask 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've got 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 flowchart
and just look at the difference, 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 which ever branch they landed on.
So it's completely separate from this if statement.
And once you've had a look at this flowchart, 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.