Here's a question, have you ever sat in your bath and wondered, why is it that no matter how forgetful
I am, my bath never overflows like this?
This is something I was thinking about the other day, and I realized that it's not because I'm particularly
diligent,
I forget stuff all the time.
I mean pizza, anyone?
It's what my pizzas usually look like.
But the reason why our bathtub or the sink doesn't overflow is because of this fantastic piece
of engineering, the overflow.
So this means that whenever the water reaches beyond a certain level about here, the water starts overflowing
so that the bathtub doesn't overflow and annoy your neighbors downstairs.
In fact, we could represent this mechanism with a conditional statement.
When the water level is, say, greater than 80cm, then it should drain the water.
But if the water level is not greater than 80cm, it should continue filling up the tub.
This type of conditional statement is known as an if / else statement.
Depending on a particular condition, we would do either A or B, and when we want to write Python code
to represent this, it looks something like this.
There's the keyword, if, and then the condition that we're testing for,
and then a colon.
And after the colon we've got an indented block of code which should be executed if this condition is
met,
if it's true, but if it's not true, then we will skip to the else block and it's just the else keyword
with a colon.
And then this code block would execute if the condition is false.
So we could represent that previous bathtub situation with code that looks a bit like this.
Let's say our water level is at 50cm,
and then we would test if the water level is greater than 80cm.
Well, if that is the case, then we should drain the water,
but if it's not greater than 80cm,
in other words, else.
Well, in this case, we should just continue filling up the bathtub.
Let's put this into practice with a real life problem.
Now, let's say that you've gotten a job at a theme park, and your first job of the day is to write
some code that replaces the ticket box.
Now there's a couple of things that you'll need to think about.
Firstly, in order for somebody to actually purchase a ticket to go on the roller coaster ride, they
will need to be over 120cm.
So we have to check what their height is, because if they're too short, then we won't be able to sell
them a ticket anyways.
In the Course resources I've included a link to this flow chart that I've created on Draw.io.
This is a really, really useful tool for creating any sort of flow charts or diagrams, and it's really
easy to use.
Now, if we take a look at this flow chart, this is basically the logic that we have to program using
our if and else statements.
If the person who is trying to purchase a ticket is not over 120cm, then they can't ride on the roller
coaster,
but if their height is greater than 120cm, then they can ride.
So let's try it out.
All that I've got here is a print statement that says, "Welcome to the roller coaster!" as well as an
input asking the user for their height in centimeters and then converting the string into a whole number,
an integer, and then I'm storing it inside this variable called height.
Now we're going to check whether if the height that the user is typed in is greater than 120.
So we use the keyword if and then we check if the height is greater than 120,
and then we add the colon.
And now when I hit Enter, you'll notice that the code editor has automatically indented me slightly
over.
I'm not over here, because in Python the spacing and indentation is really, really important.
It tells the computer that the code that I'm about to write is what should be executed when this condition
is met.
So what should happen if your height is over 120?
Then we will just print,
"You can ride the roller coaster."
But what should we print if height is not greater than 120?
Well, in this case we would use the else statement to catch when that happens.
And it's really important that you don't write the else here because this is indented over.
Instead, you want it to be at the same indentation level as the if statement.
These two are essentially a pair if and else.
Now after the else keyword, we again add a colon, and then we hit enter.
And we're now indented again.
And here we can write the code that should happen if this condition is false.
"Sorry,
you have to grow taller before you can ride."
So the really important things here are the condition which we're testing for, is the value of height
greater than 120.
The syntax of this code.
So the keywords if and else, as well as the colons that come after each of these lines.
And finally also the indentation.
Everything that is indented after the if is a block of code.
So this is indented and it's effectively inside this if.
So this line of code lives inside the else statement.
This block of code lives inside the if statement.
and if you mess up on the indentation, then you're probably going to get an IndentationError telling
you that this line five probably should be indented.
But when we do actually correct it and we hit Run, let's say that our height is 130cm,
then we get back,
"You can ride the roller coaster." But if our height was, say, 90cm, then we get a different outcome.
We get, "Sorry you have to grow taller before you can ride."
So by using if and else statements, we're able to get our code to do different things, either printing
this line or printing this line depending on a condition that we're testing for.
Now, when we use this greater than sign (>) effectively, what we're saying is that is the height greater
than 120, which means that it does not include 120.
In fact, if I run this code and if I type in my height as 120, then it actually goes into the else
block and prints this.
So if we wanted to include 120cm so that all the people who are exactly 120cm can ride the roller coaster,
then instead of just using a greater than symbol, we have to write greater than or equals (>=).
So these two symbols have to be next to each other.
And now when I run my code and I write 120, you'll see that it's now falling into this block of code.
And it's telling me that I can ride the roller coaster.
Now, these are called comparison operators, and we've already seen Greater Than.
So Lesser than is pretty self-explanatory, but we've also seen greater than or equal to and lesser
than or equal to.
So if you want to include a particular number or particular value when you do these comparisons, you
would be using these instead.
Now in this table there's also the equal to and not equal to.
For example, if you wanted to check if somebody's height is equal to precisely 120, then you would
use two equal signs.
And very often it gets a little bit confusing, especially if you're new to programming when you're
typing equal signs.
Because sometimes we're typing one and other times we're typing two.
It's important to remember that when you have one equal sign, it means that you're assigning this value
to this variable.
But when you have two equal signs, you're checking to see if the value on the left is equal to the
value on the right.
And they're completely different.
Now, the good thing is that when you get it wrong, usually you'll get enough clues in the error to
actually hint to you, hey, maybe there's something that's wrong here, right?
Because the syntax doesn't look right.
For this to be a condition, it has to be something that evaluates to true or to false.
And height equals 120 does not evaluate to either.
So by changing it to this we're saying if height is equal to 120 then we'll execute this line of code
or any other lines which are in the same block of code.
But if the height is not equal to 120, then we're going to execute this block of code.
So if I write 120 then that works.
But if I write 121 then it does not work.
Similarly, you can also check for not equals to which just flips it around.
But in our case it makes sense to say if the height is greater than or equals to 120, then you can
ride the roller coaster, but otherwise, you cannot ride the roller coaster. By using if and else statements,
we can get our computer to do different things and respond differently depending on different conditions.
So in the next lesson, I've got a coding exercise for you,
I'll see you there.