Ex29.9 Else and If

If条件格式的进一步应用

上一个练习,初步了解If语句的用法。在本练习,对IF语句进一步熟悉。如果有多个条件需要判断,多种情况需要罗列,IF语句如何应对?且看代码。

示例代码Ex30

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
people  = 30
cars  = 40
trucks = 15
if cars > people:
    print "We should take the cars."
elif cars < people:
    print "We should not take the cars."
else:
    print "We can't decide."
if trucks > cars:
    print  "That's two many cars."
elif trucks < cars:
    print "Maybe we could take the truck."
else:
    print "We still can't decide."
if people > trucks:
    print "Alright, lets's just take the trucks."
else:
    print "Fine, let's stay home then."

运行结果如下:

小结

elif 的用法是本练习关注的重点。

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