FOR、IFy以及while
(一)、IF语句
peole=20
cats=30
dogs=15
if peole<cats:
print("Too many cats! The world is doomed!")
if peole>cats:
print("Not many cats! The world is saved!")
if peole<dogs:
print("The world is drooled on!")
if peole>dogs:
print("The world is dry!")
dogs+=5
if peole>=dogs:
print("People are greater than or equal to dogs.")
if peole<=dogs:
print("People are less than or equal to dogs.")
if peole==dogs:
print("People are dogs.")
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.
Q&A
1、if 对它下面的代码起什么作用?
if 起判断作用,如果判断为true,则执行下面的代码
2、为什么 if 对下面的代码要缩进4个空格?
缩进4个空格表示是此个 if 的主体,即此 if 判断控制的代码。
3、如果 if 下面代码没有缩进会发生什么?
没有缩进的话,则出现错误提示IndentationError: expected an indented block
表示此个 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 could 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.")
if (cars>people|trucks<cars):
print("We should take the cars or trucks.")
We should take the cars.
Maybe we could take the trucks.
Alright,let's just take the trucks.
We should take the cars or trucks.
IF 嵌套使用
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("What do you do?")
print("1.Take the cake.")
prin