python和basic语言的区别_[Python Basic] 什么是条件语句(2)

副标题: 18. What are Conditionals (2)这个专栏的文字, 不仅仅是学习 Python 的基础知识, 同时, 也按照 2/8 规律学习关键知识的关键部分 - python 核心英语词汇. 我们开始这一节的正文.

elif 语句

elif 条件语句举例:

your_score = 89

passing_score = 50

credit_score = 70

distinction_score = 90

if your_score >= distinction_score:

print("Congrats!")

print("You have passed with DISTINCTION!")

elif your_score >= credit_score: ## 运行这一句

print("Congrats!")

print("You have passed with CREDIT!")

elif your_score >= passing_score:

print("Congrats!")

print("You have passed!")

else:

print("You did not pass.")

print("Please try again next time.")

print("Thank you for taking the exam.") ## 并且运行这一句

## outpu:

Congrats!

You have passed with CREDIT!

Thank you for taking the exam.当我们运行这个程序时,因为你的分数是89,不大于或等于分数(90),它将不会运行打印祝贺的第一个缩进代码块,即: You have passed with DISTINCTION!

相反,它将跳到下一条 elif 语句,它检查你的分数,也就是89分,大于或等于 70. 因为第二个 elif 条件为真, 将执行第二个 elif 条件下的缩进代码块. 即: You have passed with CREDIT!

另外,"Thank you for taking the exam"也将被打印, 因为它不是任何条件的一部分,也不是缩进块的代码。

嵌套 if 语句可以在 if 语句块的代码中嵌套 if 语句,在其中指定另一个if语句。这是另一个假设中的一个假设, 或者我们所说的嵌套if语句. 英文: You can have nested if statements, where you specify another if statement, within your if statement block of codes. It's an if inside another if. Or what we called nested if statements.

嵌套 if 语句举例.

例子中嵌套语句为:if your_score >= distinction score:

your_score = 89

passing_score = 50

credit_score = 70

distinction_score = 90

if your_score >= distinction_score:

print("Congrats!")

print("You have passed with DISTINCTION !")

elif your_score >= credit_score:

print("Congrats!")

print("You have passed with CREDIT!")

if your_score >= distinction_score -5: ## 本行是嵌套条件. (加减的优先级 高于 比较运算符)

print("You are less than 5 points to scoring DISTINCTION! ")

elif your_score >= passing_score:

print("Congrats! ")

print("You have passed!")

else:

print("You did not pass.")

print("Please try again next time.")

print("Thank you for taking the exam.")

## output:

Congrats!

You have passed with CREDIT!

You are less than 5 points to scoring DISTINCTION!

Thank you for taking the exam.第一步:

当我们运行这个程序时,因为你的分数是89,不大于或等于 distinction_score(即90),则它将不会运行打印 Congrats 的第一缩进代码块,You have passed with DISTINCTION !。 它将跳到下一个 elif 语句,该语句检查您的分数(89)是否大于或等于credit_score(即 70 )。 因为第二个 elif 条件为 True,将执行第二个ELIF条件下的缩进代码块。 因此,1.Congrats,2.You have passed with CREDIT!,将被打印出来。

第二步:

现在, 第二个 elif 声明中,您有另一个if语句(嵌套 if 语句),它检查您的分数是否为89, 大于或等于 distinction_score -5,即85。因为此嵌套的 if 条件为 True, 将执行此嵌套 if 语句下的缩进代码块。 因此,You are less than 5 points to scoring DISTINCTION!,将被打印出来。

第三步:

另外,Thank you for taking the exam, 也将被打印, 因为它不是任何条件的一部分,也不是缩进代码块的一部分

词汇

本节无新增词汇, 你的英语更棒了!

Python 的有关运算符比较级的参考链接:

发布时间: 2020 年 2 月 27 日

知乎链接:https://zhuanlan.zhihu.com/p/109590935

当前可任意转载,转载请保存以上信息即可。无需获得授权.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值