if & else
习题29 - 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 or dogs.")
if people<=dogs:
print("people are less than or equal to dogs.")
if people==dogs:
print("people are dogs.")
结果输出:
Too many cats!The world is doomed!
The world is dry!
people are greater than or equal or dogs.
people are less than or equal to dogs.
people are dogs.
进程已结束,退出代码0
Tips:
if语句是什么?有什么用处?
每条 if 语句的核心都是一个值为True或False的表达式,这种表达式被称为条件测试。Python根据条件测试的值为True还是False来决定是否执行 if 语句中的代码。如果条件测试的值为True,就执行紧跟在 if 语句后面的代码;如果值为False,Python就忽略这些代码。
可用于比较两边条件、数字或者多个条件、特定值是否相等(==) 和不相等(!=),还有大于(>)、大于等于 (>=)、小于 (< )和小于等于(<=)
+=是什么意思?
+=是递增运算符,X += 1 和 X = X + 1 相同,同理±是递减运算符
习题30 else和if
代码如下:
people=30
cars=40
trucks=15
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 trucks > cars:
print("That's too many trucks.")
elif trucks < cars:
print("Maybe we should take the trucks.")
else:
print("We still can't decide.")
if people > trucks:
print("Alright,Let's just take the trucks.")
else:
print("Fine,Let's stay home then.")
结果输出:
We should take the cars.
Maybe we should take the trucks.
Alright,Let's just take the trucks.
进程已结束,退出代码0
Tips:
- 单向选择:if…;
- 双向选择:if…else;
- 多向选择:if…elif…else;
- if语句的嵌套
对于多向选择,程序会从第 1 个 if 语句开始判断,如果第 1 个 if 语句的条件不满足,则判断第 2 个 if 语句的条件……直到满足为止。一旦满足,就会退出整个 if 结构。
if 语句的嵌套也是很好理解的,就是在 if 或 else 内部再增加一层判断条件。对于 if 语句的嵌套,一层一层从外到内地进行判断就可以了,就像剥洋葱一样.
对于 if 语句,还有以下 3 点需要说明:
- Python使用的是 elif,而不是 else if
- Python中 if 的后面不需要加括号
- Python只有 if 语句,没有 switch 语句,这一点和其他语言不同。
习题31
代码如下:
print("""You enter a dark room with two doors.
Do you go through door #1 or door#2?""")
door = input(">")
if door=="1":
print("There's a giant bear here eating a cheese cake.")
print("1.Take the cake.")
print("2.Scream at the bear.")
bear=input("<")
if bear=="1":
print("The bear eats your face off, Good job!")
elif bear=="2":
print(f"well,doing {bear} is probably better.")
print("Bear runs away.")
elif door=="2":
print("You stare into the endless abyss at Cthulhu's retina.")
print("1. Blueberries.")
print("2.Yellow jacket clothespins.")
print("3. Understanding revolvers yelling melodies.")
insanity=input(">")
if insanity=="1"or insanity=="2":
print("Your body survives powered by a mind of jello.")
print("Good job!")
else:
print ("The insanity rots your eyes into a pool of muck.")
print("Good job!")
else:
print("You stumble around and fall on a knife and die. Good job!")
结果输出:
You enter a dark room with two doors.
Do you go through door #1 or door#2?
>1
There's a giant bear here eating a cheese cake.
1.Take the cake.
2.Scream at the bear.
<2
well,doing2 is probably better.
Bear runs away.
Tips:
- 怎么判断一个数是否处于某个值域?
经典语法:1<x<10或1<=x<10
第2种方法:用x in rang(1,10)也可以!