Learn Python The Hard Way学习(29) - 什么是If
2012-06-27 15:36
549 查看
下面我们学习if语句,输入下面的代码,确保能够正确运行。
运行结果
root@he-desktop:~/mystuff# python ex29.py
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.
加分练习
通过上面的练习,我们自己猜测一下if语句的作用,用自己的话回答下面的问题。
1. 你认为if对它下面的代码做了什么?
判断为True就执行它下面的代码,否则不执行。
2. 为什么if下面的代码要缩进4个空格?
为了表示这些代码属于if判断下包括的代码。
3. 如果不缩进会发生什么?
会提示一个缩进错误。
4. 你可以从第27节中拿一些布尔表达式来做if判断吗?
5. 改变people,dogs,cats变量的值,看看会发生什么?
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."
运行结果
root@he-desktop:~/mystuff# python ex29.py
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.
加分练习
通过上面的练习,我们自己猜测一下if语句的作用,用自己的话回答下面的问题。
1. 你认为if对它下面的代码做了什么?
判断为True就执行它下面的代码,否则不执行。
2. 为什么if下面的代码要缩进4个空格?
为了表示这些代码属于if判断下包括的代码。
3. 如果不缩进会发生什么?
会提示一个缩进错误。
4. 你可以从第27节中拿一些布尔表达式来做if判断吗?
5. 改变people,dogs,cats变量的值,看看会发生什么?
相关文章推荐
- Learn Python The Hard Way学习(30) - Else和If
- Learn Python The Hard Way学习(10) - 那是什么?
- Learn Python The Hard Way学习(22) - 到现在我们已经学到了什么?
- Learn Python The Hard Way学习(50) - 你的第一个网站
- Learn Python The Hard Way学习(6) - 字符串和文本
- Learn Python The Hard Way学习(26) - 恭喜,可以进行期中考试了!
- Learn Python The Hard Way学习(7) - 更多的打印
- Learn Python The Hard Way学习(27) - 记忆逻辑关系
- Learn Python The Hard Way学习(44) - 继承还是组成?
- Learn Python The Hard Way学习(8) - 打印,还是打印
- Learn Python The Hard Way学习(28) - 布尔运算练习
- Learn Python The Hard Way学习(9) - 打印,打印,打印
- Learn Python The Hard Way学习(45) - 自己做个游戏
- Learn Python The Hard Way学习(11) - 提问
- Learn Python The Hard Way学习(18) - 命名,变量,代码,函数
- 学习笔记--python(1)--learn python the hard way
- Learn Python The Hard Way学习(12) - 输入提示
- Learn Python The Hard Way学习(13) - 参数,解包,变量
- Learn Python The Hard Way学习(19) - 函数和变量
- Learn Python The Hard Way学习(33) - While循环