• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 20/44 演示：条件逻辑

Demo: Conditional Logic | Python for Beginners [20 of 44]

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.

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 gonna 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, 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,
0.5元价格对应的税时
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
Python正是通过这些缩进来知道
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

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 enter a price of 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,

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 lowercase, it’s says, “Great.

so you must like hockey,” and I’m like,

“Yes, you are right Python, I do.”
“没错Python 我爱冰球”
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, oops,

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.