python中的if、else、elif语句用法简明讲解

下面我们学习if语句,输入下面的代码,确保能够正确运行。

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 people:
print “we should take the cars.”
elif cars < people: print "we should not take the cars." else: print "we can't dicide." 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." if people > buses:
print “alright, let’s just take the buses.”
else:
print “fine, let’s stay home then.”

运行结果

root@he-desktop:~/mystuff# python ex30.py

we should take the cars.
maybe we could take the buses.
alright, let’s just take the buses.

Posted in 未分类

发表评论