Day 2 条件语句 循环语句

本文深入探讨了Python中的条件语句(if, if-else, if-elif-else, assert)和循环语句(while, while-else, for, for-else),包括range和enumerate的使用方法,以及break、continue和pass的特殊作用。通过两个实践案例,读者可以更好地理解这些语句的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Day 2 条件语句 循环语句

条件语句

  • if

  • if-else

  • if-elif-else

  • assert

    当这个关键词后边的条件为 False 时,程序自动崩溃并抛出AssertionError的异常。

循环语句

  • while
  • while-else
  • for
  • for-else
  • range
  • enumerate
    枚举 给元素一个索引值
  • break
  • continue
  • pass
    空操作 不执行语句

练习1

i=1500
while i<=2700:
    if i%7==0 and i%5==0:
        print(i)
    i=i+1

练习2

v1,v2,t,s,l = map(int,input().split())
k=0
time=0
s1=0
s2=0
while k<l:
    s2=s2+v2
    s1 = s1 + v1
    time=time+1
    k = max(s1, s2)
    if k>=l:
        break
    if s1-s2>=t:
        s1=s1-v1*s
if k==s1:
    if k==s2:
        print("D")
    else:
        print("R")
elif k==s2:
    print("T")
print(time)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值