python循环结构之while循环

2023-02-12,,

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

python循环结构之while循环的相关教程结束。

《python循环结构之while循环.doc》

下载本文的Word格式文档,以方便收藏与打印。