Exercise 29:If 是什么

来源:互联网 发布:网络推广威客 编辑:程序博客网 时间:2024/06/03 15:59

原文链接:http://learnpythonthehardway.org/book/ex29.html

       下面你将进入的这个Python脚本向你介绍了 if 语句的使用。输入这些内容,并正确运行该脚本,然后我们来看看你的练习是否有了回报。

people = 20cats = 30dogs = 15if 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 += 5if 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."

输出结果如下:

E:\>python ex29.pyToo many cats! The world is doomed!The world is dry!People are greater than or equal to dogsPeople are less than or equal to dogs.People are dogs.

研究训练:

        在这次的研究训练中,试着猜猜if语句有什么作用。在进行下一个练习之前试着用自己的话回答这些问题:
1、你认为if对其下面的代码做了什么?
2、为什么if下面的代码需要4个空格的缩进?
3、如果不缩进会发生什么?
4、你能将Exercise 27中的其他布尔型表达式房子 if 遇见中吗?试一下。
5、看看如果改变 people ,cats ,dogs初始化值会发生什么。

学生遇见的常见问题:


+= 是什么意思?
答: x += 1 就等同于 x = x + 1 只是一种简写方式而已。你可以称这种为“按单位递增”操作符。 -= 操作符也是一样的,还有很多其他类似的表达式你在后面可以学习到。

0 0
原创粉丝点击