>> So now let's go take a look at some code
using those conditional statements in our if statements.
So if we take a look at this code here,
this is the code I'm using to calculate the tax rates in Canada.
As I mentioned in Canada,
you don't pay tax on an item unless it costs at least one dollar.
So what I'm going to do, is I'm going to ask
a user how much did they pay,
then I'm going to convert that to a number thinking
back to what we learned about datatypes and working with numbers.
We want to treat this as a number,
but the input statement always return strings.
So I am just converting that price into a number
here and then I'm just
going to say if that price is over a dollar,
then the tax is 0.07,
so seven percent, and then I'm just going to print
the tax rate on the screen.
So let's see what happens when you actually try to run that code,
and we're going to do call Python
and we're going to call check tax.
If we pass in a price of
$20 which is definitely more than one dollar,
we should see tax rate of
0.07 and sure enough it comes back tax rate is 0.07,
whereas if we enter a tax if I paid $0.50 for something,
you'll notice it does not come back and print anything out at all
because both these two statements are indented.
So neither of these statements is
executed unless the condition is true.
So they have these four spaces,
that's how Python knows which lines to
execute when the condition is true, that indentation.
Now, we can add an else statement to this.
So I might want to say if the price is under a dollar,
then let's not charge any tax.
So I've just added some logic here exactly the same code,
but all I've done is said otherwise with
an else statement and there's that colon,
don't forget the colon at the end of your statement.
At the end of your statement I'm forever forgetting that,
it's one of my most common syntax errors.
Then otherwise the tax equal 0.
So now if I run this code,
I will just clear screen start off again
Python add_else.py and how much should I pay if I paid $50,
then the tax rate comes back as 0.07.
So that's correct. If I go ahead and I run it and I pay 0.50,
it comes back and says the tax rate is 0.
So now this is a little cleaner,
a little more elegant because I've got a tax value that's
set according to any possible input.
Now, one of the other things I mentioned is that if you wish,
one of the different ways you could do this is I could
write this code exactly the same,
by simply taking the print statement because printing
the tax rate I always want to print
the tax rate regardless of what the tax is.
The only thing that changes is what I assign the tax rate to.
So in my if statement, I say if the price is over a dollar,
set the tax rate seven percent.
If the price is under a dollar,
set the tax to zero.
Then, regardless of what the final tax rate was,
print that on the screen.
So by taking this out of the if statement and it's not indented,
that means this statement will be executed all the
time no matter what happens in the if statement.
So now when I run this code and now I enter $50,
you'll still see exactly the same output,
tax rate is seven percent.
If I enterprise of 0.25 cents,
you'll see tax rate comes at zero.
So the same things happening in my code,
I'm just using a different way to achieve it.
Now, there's one other example I wanted to do,
and that was showing you the case sensitivity.
Right now I have a little line of code here that says,
please enter the name of your home country
and if the country is Canada,
then you must like hockey.
"Hey, I'm hockey Geek Girl on Twitter for reason.
I am a Canadian, I love my hockey,
I fit the stereotype."
Otherwise we say, "Okay. You're not from Canada."
So if I run this code comparing strings is the name of my file,
enter name my country and I enter
Canada as long as I entered all lowercases, it's says, "Great.
So you must like hockey," and I'm like,
"Yes, you are right Python, I do."
But if I run it and I happened to enter uppercase letters,
then it comes back and says I'm not from Canada.
So this is a case where I have to
remember that in Python when you're comparing two strings,
they're not equal to each other if one has
uppercase letters and one has lowercase letters.
So what I can do, is I can take the value that was passed in,
convert that to lowercase,
and then that returns
a lowercase version where I typed in which
will match the lowercase string Canada.
So now even if I type in books,
I need to save that file that would help.
It's correct that, just realize I hadn't actually
hit "Save" using control S to save.
Now when I run it and I enter Canada,
even if I enter all uppercase letters,
it still comes back and recognizes that I am from Canada,
so I must like hockey.
So there you have it.
Now let's move on and look at
some more complicated situations we can deal with in conditions.