while循环
2022/3/1 6:24:52
本文主要是介绍while循环,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
循环打印3次 hello
times = 3 # 定义次数为3 while times > 0 : print(times) times -= 1 # 对 times 进行一个 -1 的操作
循环打印出10,9,8,7,6,5,4,3,2,1
times = 10 while times > 0: print(times) times -= 1
循环打印出 1,2,3,4,5,6,7,8,9,10
times = 1 while times < 11: print(times) times += 1
打印出 1+2+3+4...+10 的结果
times = 1 result = 0 # 存储计算的结果,定义的值是0,不影响计算的结果 while times < 11: result += times # 把每一次循环的times的值,累积到 result 中 times += 1 print(result)
打印出a中的每个元素
a = [11,22,33,44,1,2,3,4]
index = 0 # 下标 从0开始
while index < len(a) :
print(a[index])
index += 1
打印出列表里面所有的偶数
a = [11,22,33,44,1,2,3,4] index = 0 # 下标 从0开始 while index < len(a) : if a[index] % 2 == 0: # 判断 数字 除以2 的余数 是否为0 print(a[index]) index += 1
分别打印出奇数和偶数
a = [11,22,33,44,1,2,3,4] jishu = [] oushu = [] index = 0 # 下标 从0开始 while index < len(a) : if a[index] % 2 == 0: # 判断 数字 除以2 的余数 是否为0 oushu.append( a[index] ) else: jishu.append( a[index] ) index += 1 print(jishu) print(oushu)
while 条件 True/False:
while True: print('hello')
这篇关于while循环的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)