Now up to this point, you've seen us use if statements,
else statements, elif statements,
multiple if statements as well as nested if statements.
But the thing that we haven't been able to do thus far is to check for multiple
conditions in the same line of code.
So how would we be able to combine different conditions and say, well,
is the pizza large and the user wants pepperoni and extra cheese all in the
same line of code? Well,
to do this we would need to learn about the logical operators.
So there's three of them that are really useful: and, or and not.
So let's take a look at the first one.
When you combine two different conditions using an and operator,
they both have to be true,
both A and B for the entire line of code to be true.
If just one of them is true, say A is true and B is false,
or A is false and B is true, then the overall thing evaluates to false.
So if we take a look just inside the console,
and in fact, I'm going to clear the console, and I write some code here.
Let's say that, um,
A = 12 right? Now let's say that I wanted to check whether if is A greater
than 15? Well that is going to be false.
Now what about is A greater than 10? Well,
that's going to be true.
Now if I combined this using an and statement, I could say,
well is A greater than 10 and A is less than 13 and hit enter, then I would get
true because both A is greater than 10 and A is less than 13 are true.
So in this case, when both conditions are true,
then this entire line gets evaluated to true.
But see what happens when just one of them is false.
So is A greater than 15 and A less than 13? A is greater than 15 is false,
A is less than 13 is true and now we get false.
So that's what happens when you combine different conditions using and. Now if
you only needed one of the conditions to be true,
then you could use the or operator instead.
So if a C or D were true or if they're both true,
then it'll evaluate to true.
It's only when both C and D are false
does this statement actually become false.
Now the final one is the not operator and all that this does is it basically
reverses a condition. So if the condition is false,
then it becomes true. If it's true, then it becomes false.
If I wrote not
and then I wrote the condition A is greater than 15. A greater than 15 is
definitely false, 12 is less than 15. But by putting the not there
it's going to reverse this, so going from false to true.
So when I hit enter, you'll see it is now true.
Coming back to our rollercoaster ticketing,
let's say that the rollercoaster company decided that for everybody who was
having a midlife crisis, they would give them free tickets.
And according to Wikipedia,
midlife crises typically occur when you're a 45 to 55 years old.
Let's see if we can incorporate this into our code.
So let's say that in addition to these existing price categories,
what if you had to add a separate price category for those people who are aged
between 45 and 55 and those people get to ride for free?
Do you think you would be able to change the code using what you've learned
about logical operators in order to incorporate this addition to our program?
Pause the video and give that a go.
So we've currently got three conditions: age,
less than 12, age between 12 and 18, and finally everybody else.
Now instead of just finishing up there,
let's go ahead and add another elif.
And here we're going to combine two conditions.
We're going to say if the age is greater than or equal to 45 and the age is less
than or equal to 55, then we get to catch that midlife crisis window.
Well,
in this case, we're going to print something like-- we're going to say "Everything's
going to be ok. Have a free ride on us!"
And in fact,
we don't need to modify the bill in any way because we know that with
if, elif and else statements is that once this condition matches,
then everything that's inside this block,
so everything that's indented inside this elif is going to be carried out
namely printing this out,
and then it's going to skip the rest of this if/else block and continue on.
Now, if they want to photo this,
they'll have to pay $3 but at least their ticket is free.
And we've been able to do this because we know about the and logical operator.
In the next lesson,
we're going to practice using logical operators and more by completing another
code challenge. So for all of that and more, I'll see you there.