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

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

20/44 演示:条件逻辑

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

Adding Conditions
更多的条件语句
So now let’s go take a look at some code
现在我们来看看if语句相关的
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.
任何低于1元的物品都不需要缴税
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.
但这里input的返回值为字符串
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,
然后就可以说 如果价格超过1元
then the tax is 0.07,
那么tax的值就是0.07
so seven percent, and then I’m just going to print the tax rate,
即税率为7% 然后我要把税率
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
这里我们调用Python
and we’re going to call check_tax.
然后调用check_tax文件
If we pass in a price of $20,
如果输入的价格为20元
which is definitely more than one dollar,
这样一个大于1元的值
we should see tax rate of 0.07,
我们应该看到税率为0.07
and sure enough it comes back tax rate is 0.07,
当然这里确实得到了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.
现在我们可以加入else了
So I might want to say if the price is under a dollar,
我想做的是 如果价格低于1元
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,
用else来表示 后面加上冒号
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.
那否则税率就为0
So now if I run this code,
所以我现在运行的话
I will just clear screen start off again
我先清空屏幕 然后开始
Python add_else.py,
先输入Python add_else.py
and how much should I pay if I paid $50,
然后我要知道的税率对应的价格是50元
then the tax rate comes back as 0.07.
然后就得到7%的税率
So that’s correct. If I go ahead and I run it and I pay 0.50,
结果正确 如果我要继续得到0.5元时的税率
it comes back and says the tax rate is 0.
这里就会得到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,
只是改改这个print的表达式
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,
所以在我的表达式中 如果价格超过1元
set the tax rate seven percent.
税率就设为7%
If the price is under a dollar,
低于1元的话
set the tax to zero.
就设为0
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,
所以通过把这一句拿到if之外并且不加缩进
that means this statement will be executed all the time
那么无论if中发生了什么
no matter what happens in the if statement.
这句表达式总是会被执行
So now when I run this code,
所以当我运行代码时
and now I enter $50,
输入50元
you’ll still see exactly the same output,
你会看到完全一样的输出
tax rate is seven percent.
税率还是7%
If I enter a price of 25 cents,
如果输入价格是25分
you’ll see tax rate comes at zero.
你会看到输出税率为0
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,
这里我的文件名是comparing_strings
enter name my country and I enter canada,
输入我的国家的名字 就输入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,
我必须记住 当使用Python比较两个字符串时
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.
就能和字符串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.
点击保存 crtl+s就行
Now when I run it and I enter Canada,
现在我运行 然后输入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.
可以使用条件语句解决的更加复杂的情况

发表评论

译制信息
视频概述

这一次继续探讨了条件逻辑语句的使用

听录译者

收集自网络

翻译译者

伽卡

审核员

审核员MS

视频来源

https://www.youtube.com/watch?v=zqVmqtTLmgw

相关推荐