>> So in the last module,
I showed you how we can use conditions to do
things like calculating the taxes.
Well, it turns out calculating the taxes
in Canada is a little more complicated than that.
One aspect is with the prices,
but we also charge different taxes
in different provinces across the country.
So sometimes I have
multiple conditions and I
need to figure out what's the correct action.
So let's say you're in the province of Alberta or Nunavut,
then you actually have a tax rate of five percent in Canada.
If you're in the province of Ontario
then the tax rate is 13 percent.
So it's a big difference, you have
to make sure you calculate it correctly.
So I could write multiple if statements.
Say if the province is Alberta,
set the tax to five percent.
If the province is Nunavut,
set the tax to five percent.
If the provinces Ontario,
set the tax to 13 percent.
This will work, but there's a more elegant way to do it,
and that is using something called elif.
Because in this case,
only one of these conditions will ever happen.
The province is never going to be equal to
Alberta and Ontario at the same time,
it's going to be one or the other,
that's when you want to go with elif.
It allows me to say, hey,
check if the province is Alberta,
it's not, then check the next condition so else.
So otherwise, if the province is none of that or what is not,
then check the next condition.
If problem is Ontario, then check that one.
So basically, it'll go down
the list and when it finds one that's a match,
it'll set the tax we procreate
rate and exit the if statement right there.
So the elif allows me
to do a little more efficient way of checking code,
it's great if only one of the conditions will ever be met.
You still need that colon at the end of each statement.
The other vanished using the elif is,
it allows you to put that default action in there as well.
So I have that ability to say, hey,
for these provinces has specific rate,
but for everybody else there's a default tax rate of 15 percent.
So now, I can just add
an else statement and say if it's Alberta here's what you do,
if it's Nunavut, here's what you do,
if it's Ontario this what you do.
If it's anything else,
just at the taxed at 15 percent.
So the other advantage via
an elif is I can have that default else action.
Now, if you have more than one condition
that causes the same action,
you can actually combine those together.
So in this case, Alberta and
Nunavut actually had the same tax rate.
So what I can do is, I can use an or to say,
if the provinces Alberta or provinces equal to Nunavut,
tax should be set to five percent.
So this again just saves me some time
because I have multiple conditions causing the same output,
I can use things like an or statement
to say if it's one or the other,
then give me this following input.
Now, the way or statements work is,
if the first condition is true and the condition is true,
the condition evaluates to true.
What it really comes down to the or statements is pretty easy.
If any one of the conditions is true,
then the entire statement will be evaluated as true.
So in this case, if the provinces Alberta,
tax will be set to five percent,
if the province is Nunavut,
set the tax five percent.
It doesn't matter which one is true.
If any of them is true,
it will set the tax five percent.
For it to say,
set the tax or anything else then it cannot be
Alberta or it's not Nunavut either,
so it has to be neither one of them.
So this is what we call a truth table,
it shows how the or statements are evaluated.
If you have a list of values to check,
there's actually a neat little shortcut
for this specific situation.
There's something called the IN operator.
Basically, it's for those situations
when you find yourself saying,
if it equals this,
or it equals this,
or it equals this.
In that case, you can use something called an IN.
So it turns out and this is actually true because
actually three places in Canada which all have the same tax rate.
So I can say, if province in this list,
Alberta none Nunavut or Yukon,
so if any one of those three values,
set the tax to five percent.
I could have done that with nor statement perfectly valid,
but I find the IN statement a little bit tidier to work with,
but it's up to you effectively they do the same thing.
Now what about when
an action depends on a combination of conditions.
Let's say you're visiting Canada,
but you don't live there.
Well, you don't necessarily have to pay the taxes.
That's that whole concept of things being
duty-free when you're moving from one place to another,
so we won't charge you the taxes if you're visiting.
So we might need to say, hey,
if the country is Canada,
so if you're a Canadian making this purchase
and the province is equal to the following charges tax rate.
But if you're not a Canadian, well,
then we're going to set the tax is zero.
We want you to come to the country and buy some awesome things,
but you don't have to pay to maintain our roads.
So you can do nest if statements.
So what I have here is, I have an if statement and
inside the if statement is another if statement.
Now, I'm not just making those spaces,
those four space in indentation
to make it look pretty and more readable.
It does make my code more readable,
but that does affect the execution of my code as well.
Remember, whatever is inside or
indented is only executed if that statement's true.
So this whole if statement of calculating
the tax rate only it's checked if the country is Canada.
If you are in Canada,
then if the province is in this list,
then it's taxes is five percent.
So in that indenting there of
four spaces does change how the code is executed,
so be very careful with that
especially when you start nesting if statements together.
This might be a place where you might
find it helpful to actually put that code
into a function just so you don't make mistakes on the indenting,
and good news, we're going to show you how to do function soon.
So we've got a lot of
different real-world business rules we can
handle and the more real-world it is,
it seems the more complex the conditions apply.
I'm going to show you now how we would do this inside the code.