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)
相关推荐
黄忠12 小时前
01-系统架构设计-LangGraph状态机与多源异构RAG
python
zzzzzz31012 小时前
假如我是掘金管理员,我先给评论区装个'代码审查'系统
python·程序员·机器人
砍材农夫12 小时前
python环境|conda安装和使用(2)
后端·python
程序员龙叔1 天前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户8356290780511 天前
使用 Python 操作 Word 内容控件
后端·python
码云骑士1 天前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
闵孚龙1 天前
《PyTorch 深度修炼》Dataset 和 DataLoader:数据如何喂给模型
人工智能·pytorch·python
goldenrolan1 天前
A公司物料替代测试系统 v1.7:从需求到 exe/apk 的 AI 辅助全链路实践
android·自动化测试·软件测试·python·ai
菜板春1 天前
jupyter入门-手册-特征探索
python·jupyter