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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wws563/article/details/50621697

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."
展开阅读全文

没有更多推荐了,返回首页