• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

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

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 19/44 条件逻辑

Conditional Logic | Python for Beginners [19 of 44]

Handling Conditions

Okay. So now once we get into writing more complex code,

at some point, you’re going to need to be able to say,

when this happens do this,

when something else happens, react differently.

So that’s why we need to be able to handle conditions inside of our code.

Basically, you’ll need the ability to react differently

and take different actions based on what’s happening.

So one of the more common situations,

in Canada, we have all sorts of different tax levels

depending on which State Province you live in within the country.

In the US, it depends on what state you live in,

and it also depends on the price.

Actually, if you’re buying fast food

at a restaurant in Canada if anything costs less than \$1,

you don’t pay any tax on it.

So when we’re calculating tax,

we actually say if the price

is over a dollar or equal to a dollar,

then we charge a certain amount of tax.

So in Python, I can handle that by adding an if statement,

and you’ll notice a bit of syntax

here you’ll see the if statement.
if的一点语法
If that’s fairly obvious.

If my price is greater than or equal to \$1,

then I’m going to take the following actions.

Now, a couple of things to watch out for.

There’s always a colon at the end of your condition that is

Python-specific syntax and the indentation here.

It’s not an accident that the word tax is

moved over to the right here by about four spaces,

and it did by four spaces not a tab.

Though if you’re using Visual Studio Code,

it’ll auto correct that for you so you can get

away with using a tab in Visual Studio Code.

But try to get in the habit of making four spaces,

and anything that is four spaces in will only

be executed if the price is greater than or equal to \$1.

Now, I’m using a greater than or equal to symbol here.

There’s different symbols we use

depending on the condition we’re looking for.

I might say greater than,

less than, greater than or equal to, less than equal to.

The two most important ones

which do vary from programming language.

Programming language is equal to

would be equal equal sign,

and the not equal to in Python is an exclamation mark equal
Python中的“不等于”是叹号加等号“!=”
or bang equal depending on the term you prefer to use.
Python中的“不等于”是叹号加等号“!=”
Now, we can also add a default action using an else statement.

That’s a way of saying if this condition is met,

set the tax to seven percent.

Else, so if it’s anything else do the following.

So if the price is not over a dollar,

the rules in Canada state.

If the cost is over a dollar,

you pay a tax of seven percent what we call our service tax.

Otherwise, you don’t pay the tax.

So I can use that with an else statement.

Again, always remembering that colon.

I’ve constantly forget that when I’m writing my code

and have to correct that with the syntax error,

just as you are learning syntax errors from Christopher.

And again you have to indent, by four spaces,

the lines you want executed if that happens.

The indentation really does change execution.

I could actually write this exact same code a different way.

I basically want to say calculate the tax

and then print the tax out.

So here I say if the tax price is over a dollar,

set the tax to seven and then print it.

Otherwise, set the tax zero and print it.

Or I could just say if the price is over dollar,

set the tax seven otherwise set the zero,

and when I’m all done evaluating the correct value of tax,

then go and print the tax out.

Both of these will do exactly the same thing.

Which one should you use?

I like the one on the right.

It’s a little bit more elegant not

having the print statement repeating.

But if it’s more confusing for you,

there’s nothing wrong with the code on the left.

Now, be careful when you’re comparing strings.

They’ll get you into trouble.

So if you run this code and I’m just

trying to see if somebody is a Canadian or not.

I ask what country somebody’s from and they type in CANADA,

and I say if the country is equal to-

remembering that double equal sign means is equal to-canada.

Then print, “Oh look a Canadian”,

and obviously I set country to Canada.

But it comes back and gives me the no you are not from Canada.

It did not evaluate the country.

What went wrong?

String comparisons are case sensitive.

So when you’re saying is this string equal to this string,

if one’s in uppercase letters and ones in lowercase letters,

then Python’s going to say that’s not a match.

So how do I fix that?

We’ve got to think back to wow

the module we did a little while ago on string functions.

There’s a function we can use

that will convert a string to lowercase or to uppercase.

So what we can do is we can

take the value they give us convert that to lowercase,

and then compare that to the word canada all in lowercase letters.

So now when someone types in a value,

it doesn’t matter what they type in,

I’ll convert it to lowercase before I do the comparison,

and that will fix my error.

So this is a great example of a runtime error

that can occur in a way and I can address that and fix that with my code.

So conditions are very important and allow us to,

our code to react to different situations.

So let’s go take a look at these examples in in some code

before we move on to more complex types of If statements.