Python08 循环

复制代码
'''
    循环
    1. while
    2. for

    循环控制
    1.back 退出当前循环
    2.continue 结束本次继续下次循环
'''

i = 0
while i < 10:
    print(f'我正在学习python {i}')
    i+=1 #循环增量
print('while 结束')

while True:
    print(f'我正在学习python {i}')
    if i > 20:
        break #退出循环
    i+=1 #循环增量
print('while2 结束')

# range 参数说明(开始数值,结束数值, 步长),结束数值是必须的,步长不指定默认为1,开始数值 不指定默认为0
print('for')
for i in range(1, 11, 1):
    print(f'for1 {i}')
print('---------------------')
for i in range(1,11):
    print(f'for2 {i}')

print('---------------------')
for i in range(11):#(结束数值),
    print(f'for3 {i}')
复制代码
循环 的 else 用法
复制代码
'''
循环 的 else 用法
循环自己结束,而不是从break中结束时,执行else

while 循环 else 格式
while True:
    XXXXX
else;
    print('3次密码错误,账号冻结,请联系管理员')
'''

for i in range(3):
    password = input('请输入密码:')
    if password == "666":
        print('登录成功')
        break
    else:
        print('密码错误,请重新输入。')
else:
    print('3次密码错误,账号冻结,请联系管理员')

print('程序结束')

for s in 'hello,python':
    print("for out str : ", s)
else:
    print("for out str 正常结束")

sum = 0
index = 1
while index <= 100:
    sum += index
    index += 1 #python 没有 index ++ 的写法,注意
else:
    print('1-100和为:',sum)
相关推荐
BK贝壳2 小时前
bool-hybrid-array 的 GPU_opt 与 cjx:一个没显卡就报错的开关,一个把 800 字压到 96 字节的编码
python
BD_Marathon6 小时前
安装Jupyter notebook
ide·python·jupyter
Zlzxzw6 小时前
End-to-End Autonomous Generation of Human Assembly Plans
python
Wellijohn7 小时前
2000 个请求、30% 失败率:我把 Python 批量并发的限流和重试参数算了一遍
后端·python·性能优化
Logintern099 小时前
deep agents 多智能体模式——规划与委派
python·langchain
2601_9679801110 小时前
Python 办公自动化合集 ⑰:Excel自动计算——总和/平均分/最大值
python·pycharm·办公自动化·数据处理·excel自动计算
databook12 小时前
时间序列预测,模型换了一圈为什么没变好
python·数据分析
reasonsummer13 小时前
【教学类-07-17】20261119大3班破译电话号码HTML实际操作(添加“跳过按钮”和“返回按钮”在底部、增加“自编题目+解题”部分)
python·html
2601_9679801116 小时前
Python闭包(Closure)详解
python·作用域·装饰器·函数式编程·闭包
AI创界者16 小时前
ComfyUI 进阶实战:MiniMax-H3 视频人物脸部替换工作流全解析(对比 Face 5.5 实测与避坑指南)
人工智能·python·aigc