ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

19/44 条件逻辑 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

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,
购买了任何价格低于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,
当价格大于等于1元时
then we charge a certain amount of tax.
我们要收取一定的税款
So in Python, I can handle that by adding an if statement,
在Python中 通过加入if语句即可处理这种情况
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,
如果价格大于等于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.
这是Python的特定语法 还有这里的缩进
It’s not an accident that the word tax is
这里这个词tax被放到4个空格后面
moved over to the right here by about four spaces,
并不是意料之外的事
and it did by four spaces not a tab.
而且是4个空格 不是制表符
Though if you’re using Visual Studio Code,
不过当你使用Visual Studio Code时
it’ll auto correct that for you so you can get
它会自动帮你纠正 所以你
away with using a tab in Visual Studio Code.
在Visual Studio Code中使用制表符即可
But try to get in the habit of making four spaces,
但应该试着养成用4个空格的习惯
and anything that is four spaces in will only
所有4个空格后的语句
be executed if the price is greater than or equal to $1.
都只在价格大于等于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.
现在我们可以使用else语句增加一个默认操作
That’s a way of saying if this condition is met,
就是说如果满足条件
set the tax to seven percent.
税率就设为7%
Else, so if it’s anything else do the following.
然后“else” 如果是别的 就执行下面的
So if the price is not over a dollar,
所以如果价格没有超过1元
the rules in Canada state.
按加拿大规定不需缴税
If the cost is over a dollar,
如果超过1元
you pay a tax of seven percent what we call our service tax.
你要缴7%的税 我们称其为服务税
Otherwise, you don’t pay the tax.
否则就不需要缴税
So I can use that with an else statement.
我可以用else语句完成
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.
好像在像Christopher学习语法错误一样
And again you have to indent, by four spaces,
还有 你必须对那些要执行的行
the lines you want executed if that happens.
作4个空格的缩进处理
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,
所以这里写 如果价格超过1元
set the tax to seven and then print it.
就设置税率为7% 然后打印出来
Otherwise, set the tax zero and print it.
否则就设为0并打印
Or I could just say if the price is over dollar,
或者 如果价格超过1元
set the tax seven otherwise set the zero,
设置税率为7% 否则为0
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,
我问某人来自哪个国家然后得到输入“CANADA”
and I say if the country is equal to-
然后如果country的值等于——
remembering that double equal sign means is equal to-canada.
注意这里用两个等号表示“等于”——canada
Then print, “Oh look a Canadian”,
那打印出来的就是“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.
但最后给出来的结果却是 “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.
那Python会认为二者并不相等
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.
然后再把它和全都是小写的“canada”进行比较
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.
我们先来看几个这方面的例子

发表评论

译制信息
视频概述

条件语句用处大,我们一起来学它。

听录译者

收集自网络

翻译译者

伽卡

审核员

审核员SR

视频来源

https://www.youtube.com/watch?v=5pPKYWqkoek

相关推荐