【组队学习】Task06:FOR、IF、以及while

这篇博客详细介绍了Python中的IF语句、FOR循环和while循环的使用。IF语句用于条件判断,FOR循环常与range()函数结合,用于遍历序列;while循环则基于条件持续执行代码块。同时,文中提到了IF语句的嵌套和FOR循环中range()函数的参数解析,以及在datawhale组队学习教程中的相关学习资源。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值