每个人都该学的30个Python技巧|技巧 8:进阶判断—巢状判断式(字幕、衬乐、练习)

昨天教的是判断式,那它会用到程序区块,那Python要怎麽显示程序区块呢?是要用冒号以及缩排来显示,缩排非常非常的重要,我在这部分跌倒了好几次இдஇ,所以大家一定要特别小心!

那今天就进阶一点,巢状判断式就是在if判断式再加入if判断式,程序并没有规定只能再加几层,但是如果太贪心加太多了之後除错就会变得比较复杂,可读性就降低了。那就透露到这边,剩下的就看影片来了解吧~


影片的最後有三题练习题,我把题目放在下面了,答案会公布影片中呦~

练习1

您正在创建一个Python程序来评估英文大小写,请选择四个代码,并按照正确的顺序排列。

A. animal = input('Enter your favorite animal:')

B. if animal.lower() == animal:
       print(animal, 'is all lower case.')
       
C. else: print(animal, 'is mixed case.')

D. if animal.lower == animal:
       print(animal, 'is all lower case.')
       
E. elif animal.upper() == animal:
       print(animal, 'is all upper case.')
       
F. elif animal.upper == animal:
       print(animal, 'is all upper case.')

练习2

请观察以下程序码,并回答下列问题。

if income >= 540000:
    if income >= 4530000:
      income_tax = income * 0.4
    elif income >= 2420000:
      income_tax = income * 0.3
    elif income >= 1210000:
      income_tax = income * 0.2
    else:
      income_tax = income * 0.12
else:
    income_tax = 0

(1) 当今年收入到达多少便要开始徵收所得税?
A. 4530000
B. 2420000
C. 1210000
D. 540000

(2) 若今年收入为4500000,所得税为几%?
A. 40%
B. 30%
C. 20%
D. 12%

练习3

您正在编写一个根据消费金额获得折价券的函式,该功能必须满足以下要求:

  • 消费满1500元,获得50元折价券
  • 消费满2000元,获得100元折价券
  • 消费满2500元,获得150元折价券
___(1)___
    ___(2)___
        discount = 150
    elif purchase_amount >= 2000:
        discount = 100
    else:
        discount = 50
___(3)___

(1)
A. else:discount = 0
B. if purchase_amount >= 1500:
C. if purchase_amount >= 2500:

(2)
A. else:discount = 0
B. if purchase_amount >= 1500:
C. if purchase_amount >= 2500:

(3)
A. else:discount = 0
B. if purchase_amount >= 1500:
C. if purchase_amount >= 2500:


<<:  [iT铁人赛Day8]JAVA阵列(中篇)

>>:  [Day7]C# 鸡础观念- 你有选择障碍吗? 交给 if陈述式 解决吧!

[D06] placeholder

写在前面 test for placeholder test for placeholder tes...

Day#03 初始专案

前言 如同第一天所说,基本语法的练习实在是太无聊了。不如就马上来实作,从做中学吧₍₍ ◝(●˙꒳˙●...

Day 02 Introduction to embedded system

Embedded system(components and applications) Comp...

移动设备安全政策是防止影子 IT 使用的最佳安全控制

移动设备安全政策是防止影子 IT 使用的最佳安全控制。用户教育可促进对安全政策和问题的普遍认识,但应...

[第二十二只羊] 迷雾森林舞会XVI 策略模式 Strategy Pattern

天亮了 昨晚是平安夜 关於迷雾森林故事 甜水镇 洛神:10号玩家请继续发言 10号: 我跟5号的想法...