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)
相关推荐
懂点Tech3 分钟前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
爱上纯净的蓝天5 小时前
不用云做 RAG:一个本地笔记问答工具的取舍与坑
python·开源·大模型·rag·本地部署
cspttty5 小时前
2027风险管理岗准备路线:数学与应用数学如何补齐金融、SQL与Python
开发语言·python·金融
用户8356290780515 小时前
如何使用 Python 格式化 Excel 单元格中的长文本
后端·python
我命由我123455 小时前
Python 开发 - Python 的 pathlib
java·开发语言·后端·python·学习·java-ee·学习方法
严谨的麻辣烫6 小时前
Python HTTP 连接池怎么用?从连接复用到请求异常排查
开发语言·python·http·requests·网络排查
摇滚侠6 小时前
《Java并发编程之美》第 4 章 Java 并发包中原子操作类原理剖析 阅读笔记 7
java·笔记·python
用户8356290780516 小时前
如何使用 Python 在 Word 文档中创建和格式化列表
后端·python
GIoT80106 小时前
网络出口如何选择?固定 IP 与 IP 轮换的原理及实践
python
羡林i6 小时前
ProcessPoolExecutor 传参踩坑实测:pickle 失败的四种写法
python