您的位置:首页 > 编程语言 > Python开发

python学习日记_第十二天(ex29~30)

2016-02-02 08:36 459 查看
L29 如果(If)

If语句的练习:

#coding:utf-8
people = 20
cats = 30
dogs = 15

#下面这个用还有not的符合布朗式做判断条件
if not (1 == 10 or 3 == 4):
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."

'''
1.你认为 if 对于它下一行的代码做了什么?
判断是否执行。
2.为什么 if 语句的下一行需要 4 个空格的缩进?
这个python的格式,用来判断if控制的范围。
3.如果不缩进,会发生什么事情?
会报错
4.把习题 27 中的其它布尔表达式放到``if语句``中会不会也可以运行呢?试一下。
如果把变量 people, cats, 和 dogs 的初始值改掉,会发生什么事情?
可以。更改变量使if条件变化,执行的内容也会变化。
'''


L30 Else 和 If

#coding:utf-8
people = 30
cars = 40
buses = 50

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 buses > cars:
print "That's too many buses."
elif buses <cars:
print "Maybe we could take the buses."
else:
print "We still can't decide."
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: