Ex29 What If

条件语句

从 Ex28 介绍Boolean类型,紧接着,在本练习,用代码感受一翻。Zed 的书过半,才开始引入条件语句,确实有讲究。大概对于初学者来说,逻辑语句,不太容易接受,看看“三段论”在人群中的普及就知道大概了。 示例代码 Ex29:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
people = 20
cats = 30
dogs = 15
if people < cats:
    print "Too many cats! The world is doomed!"
if people > cats:
    print "Not many cats! The world is saved!"
if people < dogs:
    print "The world is drooled on!"
if people > dogs:
    print "The world is dry!"

dogs += 5
if people >= dogs:
    print "People are greater than or equal to dogs."
if people <= dogs:
    print "People are less than or equal to dogs."
if people == dogs:
    print "People are dogs."

运行结果如图:

小结

强制缩进是python的特色,有人可能认为这样呆板,但是对于多数新手来说,这道紧箍咒确实能训练良好的书写风格。

Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计