1 people = 20 2 cats = 30 3 dogs= 15 4 5 if people < cats: 6 print "Too many cats! The world is doomed!" 7 8 if people > cats: 9 print "Not many cats! The world is saved!" 10 11 if people < dogs: 12 print "The world is drooled on!" 13 14 if people > dogs: 15 print "The world is dry!" 16 17 dogs += 5 18 19 if people >= dogs: 20 print "People are greater than or equal to dogs." 21 22 if people <= dogs: 23 print "People are less than or equal to dogs." 24 25 if people == dogs: 26 print "People are dogs."
以上是运行结果。
study Drills
1、What do you think the if does to the code under it?
if对下一行代码进行判断true or false
2、Why does the code under the if need to be indented four spaces?
python是通过缩进的方式对代码进行区隔
3、What happens if it isn‘t indented?
3.1发生错误:IndentationError: expected an indented block
3.2让python认为if下面连接的并非是条件
4、Can you put other boolean expressions from Exercise 27 in the if-statement? Try it.
1 people = 20 2 cats = 30 3 dogs= 15 4 5 if people != cats: 6 print "Too many cats! The world is doomed!" 7 8 if people == cats: 9 print "Not many cats! The world is saved!" 10 11 if not (people and dogs): 12 print "The world is drooled on!" 13 14 if people and dogs: 15 print "The world is dry!"
5、What happens if you change the initial values for people, cats, and dogs?
结果会不一样咯~
原文:http://www.cnblogs.com/LevenLau/p/6363544.html