python循环结构之while循环

在python中,除了for循环,还有一个while循环

for循环:循环次数是明确了的

while循环:循环次数不确定,循环停止条件由用户自定义

# while语句结构
while 判断条件:
    执行语句

当判断条件为真时,则循环执行语句,否则跳出循环体,停止执行,while的判断条件与if一致,可参考if篇幅

复制代码
# 打印10以内的数
i = 0
while i <= 10:
    print(i)
    i += 1
打印结果:
复制代码
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/while_1.py
0
1
2
3
4
5
6
7
8
9
10
复制代码
复制代码

嵌套if -- else

复制代码
# 统计100以内奇数、偶数个数
i = 0
count_1, count_2 = 0, 0
while i < 100:
    if i % 2 == 0:
        count_1 += 1
    else:
        count_2 += 1
    i += 1
print("100内奇数个数为:", count_1, "偶数个数为:", count_2)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/while_1.py
100内奇数个数为: 50 偶数个数为: 50
复制代码

嵌套for循环

复制代码
# 打印100以内的质数
data = list()
i = 0
while i <= 100:
    if i <= 1:
        pass
    else:
        for j in range(2, i):
            if not i % j:
                break
        else:
            data.append(i)
    i += 1
for i in range(len(data)):
    print("100以内第", i+1, "个质数为:", data[i])
打印结果:
复制代码
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/for_1.py
100以内第 1 个质数为: 2
100以内第 2 个质数为: 3
100以内第 3 个质数为: 5
100以内第 4 个质数为: 7
100以内第 5 个质数为: 11
100以内第 6 个质数为: 13
100以内第 7 个质数为: 17
100以内第 8 个质数为: 19
100以内第 9 个质数为: 23
100以内第 10 个质数为: 29
100以内第 11 个质数为: 31
100以内第 12 个质数为: 37
100以内第 13 个质数为: 41
100以内第 14 个质数为: 43
100以内第 15 个质数为: 47
100以内第 16 个质数为: 53
100以内第 17 个质数为: 59
100以内第 18 个质数为: 61
100以内第 19 个质数为: 67
100以内第 20 个质数为: 71
100以内第 21 个质数为: 73
100以内第 22 个质数为: 79
100以内第 23 个质数为: 83
100以内第 24 个质数为: 89
100以内第 25 个质数为: 97
复制代码
复制代码

嵌套while循环

复制代码
# 打印一个三角形
i = 1
while i <= 5:
    j = 1
    while j <= i:
        j += 1
        print(" *", end="")

    print(" ")
    i += 1
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/while_1.py
 * 
 * * 
 * * * 
 * * * * 
 * * * * * 
复制代码

用户控制何时退出循环

复制代码
# 当用户输入quit时退出循环
message = input("Welcome to the test program,If you type quit, end the test,and Quit is case insensitive  ")
while message.lower() != "quit":
    message = input("type your words,Enter quit to exit the test ")
    print(message)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/while_1.py
Welcome to the test program,If you type quit, end the test,and Quit is case insensitive  
type your words,Enter quit to exit the test hello world
hello world
type your words,Enter quit to exit the test country road take me home
country road take me home
type your words,Enter quit to exit the test QuIt
QuIt
复制代码
复制代码
# 当用户输入Q退出循环
flag = 1
while flag:
    message = input("type your words,Enter quit to exit the test ")
    if message.lower() == "Q":
        flag = 0
    print(message)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/while_1.py
type your words,Enter quit to exit the test ok, I'm fun
ok, I'm fun
type your words,Enter quit to exit the test Q
Q
type your words,Enter quit to exit the test 
复制代码

 使用break 退出

break 条满足时, 直接跳出循环体,不再执行循环语句

复制代码
# 当用户输入Q退出循环
flag = 1
while flag:
    message = input("type your words,Enter quit to exit the test ")
    if message.lower() == "Q":
        break
    print(message)
打印结果:
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/while_1.py
type your words,Enter quit to exit the test I'm fun
I'm fun
type your words,Enter quit to exit the test q
q
复制代码

使用contiune

continue 条满足时,结束当前循环,进行下一轮循环

复制代码
# 打印10以内的偶数
i = 1
while i <= 10:
    i += 1
    if i % 2 != 0:
        continue
    else:
        print(i)
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/while_1.py
2
4
6
8
10
复制代码

死循环

i = 1
while i <= 10:
    if i % 2 != 0:
        continue
    else:
        print(i)
    i += 1

 

posted @   **绵绵羊**  阅读(907)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示