python:第二十七章:while循环

一,while语句:

1,语法:

while 条件表达式:
    # 循环体

 当条件表达式的返回值为真时,则执行循环体中的语句,
执行完毕后,重新判断条件表达式的返回值,
如果表达式返回的结果为假,则退出循环体

2,流程图:

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/17/python-di-er-shi-liu-zhang-while-xun-huan/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 打印从1到10
n = 1
while n <= 10:
    print(n, end=" ")
    n += 1
 
# 打印一个空行
print()
 
# 计算从1加到100的和
n = 1
sumRes = 0
while n <= 100:
    sumRes += n
    n += 1
print("1到100的和为:", sumRes)

运行结果:

1 2 3 4 5 6 7 8 9 10 
1100的和为: 5050

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/17/python-di-er-shi-liu-zhang-while-xun-huan/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

三,遍历列表

1
2
3
4
5
6
7
8
# 遍历列表
# len函数用来得到列表的长度
staffs = ["擎天柱", "补天士", "热破", "通天晓"]
length = len(staffs)
i = 0
while i < length:
    print(staffs[i])
    i += 1

运行结果:

擎天柱
补天士
热破
通天晓
posted @   刘宏缔的架构森林  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2022-11-20 javascript: 用图片加载演示promise的应用(chrome 107.0.5304.110)
点击右上角即可分享
微信分享提示