while语句

while循环是一个条件循环语句,如果while后的条件为真时,代码块一直循环,直到条件不再为真则结束循环。

while循环的语法如下:

while expression:

suite_to_repeat

下图可简单说明while语句循环的执行过程:

回顾:python规定

①任何非数字0和非空对象都为True。

②数字0,空(null)对象和特殊对象None都为False。

③True,False首字母必须大写。

例子:

>>> not 0
True
>>> not 1
False
>>> not []
True
>>> not [0]
False
>>> not [1]
False
>>> not True
False
>>> not False
True
1、while语句用法
(1)一般用法,计数循环。
①正序,样例源文件:
strs = "Hello World."
count = 0
while count <= 11:
print(strs[count],end=' ')
count += 1
print()
print('end')
执行结果:
H e l l o W o r l d .
end
②倒序,样例源文件:
strs = "Hello World."
count = 11
i = 11
while (i <= 11 and i >=0):
print(strs[count-i],end=' ')
i -= 1
#break
print()
print('end')
执行结果:
H e l l o W o r l d .
end
(2)无限循环
①当条件判断为布尔真时,无限循环。
样例源文件:
while True:
print("Hello World.")
执行结果:
Hello World.
Hello World.
Hello World.
Hello World.
Hello World.
Hello World.
……
②当条件不变时,陷入死循环。
样例源文件:
num = 1
while num == 1:
print("Hello World.")
执行结果:
Hello World.
Hello World.
Hello World.
Hello World.
Hello World.
Hello World.
……
(3)while…else…,else后的代码块为条件假时执行。
样例代码:
>>> while False:
... print("hello world.")
... else:
... print("nihao python.")
...
nihao python.

2、pass、continue、break在while语句中的用法

(1)pass在while语句和for语句用法相同,仅占位,能避免格式错误。

样例源文件:

strs = "Hello World."
count = 0
while count <= 11:
print(strs[count],end=' ')
print("此时索引为{}".format(count))
pass
count += 1
print()
print('end')
执行结果:
H 此时索引为0
e 此时索引为1
l 此时索引为2
l 此时索引为3
o 此时索引为4
此时索引为5
W 此时索引为6
o 此时索引为7
r 此时索引为8
l 此时索引为9
d 此时索引为10
. 此时索引为11

end

(2)continue在while语句中表示结束本次循环并开始下一次循环,直到条件为False时,结束循环。

①样例源代码:

strs = "nihao python."
count = 0
while count <= 12:
print(strs[count],end=' ')
print("此时索引为{}".format(count))
continue
count += 1
print()
print('end')
执行结果:
n 此时索引为0
n 此时索引为0
n 此时索引为0
n 此时索引为0
n 此时索引为0
n 此时索引为0
……
循环每次到continue即开始下一次循环,使得每次count一直为0,陷入了死循环。

②样例源代码:

strs = "nihao python."
count = 0
while count <= 12:
print(strs[count],end=' ')
count += 1
continue
print("此时索引为{}".format(count-1))
print()
print('end')
执行结果:
n i h a o p y t h o n .
end

(3)break在while语句中表示结束循环。

样例源代码:

strs = "BaDianGangHuo."
count = 0
while count <= 13:
print(strs[count],end=' ')
print("此时索引为{}".format(count))
break
count += 1
print()
print('end')
执行结果:
B 此时索引为0

end

3、while语句编写九成九乘法表

①我最初学的乘法表,源代码:

i = 1
while i <= 9:
j = 1
while j <= i:
#print("{}*{}={}".format(j,i,i*j),end = ' ')
print("%d*%d=%-2d"%(j,i,i*j),end = ' ') # '-2'代表靠左对齐,两个占位符
j += 1
print()
i += 1
执行结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
②把①上下颠倒的乘法表,源代码:
i = 9
while i >= 1:
j = 1
while j <= i:
print("%d*%d=%2d"%(j,i,i*j),end = ' ')
j += 1
print()
i -= 1
执行结果:
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25
1*4= 4 2*4= 8 3*4=12 4*4=16
1*3= 3 2*3= 6 3*3= 9
1*2= 2 2*2= 4
1*1= 1
③把②左右颠倒的乘法表,源代码:
i = 9
while i >= 1:

k = 9
while (k-i) > 0 :
print(' ',end = '') #算上空格一组式子占用7个空格,比如8*8=64 所以空7个格
k -= 1

j = i
while j >= 1:
print("%d*%d=%2d"%(i,j,i*j),end = ' ')
j -= 1
print()
i -= 1
执行结果:
9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1= 9
8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1= 8
7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1= 7
6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1= 6
5*5=25 5*4=20 5*3=15 5*2=10 5*1= 5
4*4=16 4*3=12 4*2= 8 4*1= 4
3*3= 9 3*2= 6 3*1= 3
2*2= 4 2*1= 2
1*1= 1
④从大到小排序的乘法表,源代码:
i = 9
while i >= 1:
j = 9
while j>= i:
print("%d*%d=%2d"%(j,i,i*j),end = ' ')
j -= 1
print()
i -= 1
执行结果:
9*9=81
9*8=72 8*8=64
9*7=63 8*7=56 7*7=49
9*6=54 8*6=48 7*6=42 6*6=36
9*5=45 8*5=40 7*5=35 6*5=30 5*5=25
9*4=36 8*4=32 7*4=28 6*4=24 5*4=20 4*4=16
9*3=27 8*3=24 7*3=21 6*3=18 5*3=15 4*3=12 3*3= 9
9*2=18 8*2=16 7*2=14 6*2=12 5*2=10 4*2= 8 3*2= 6 2*2= 4
9*1= 9 8*1= 8 7*1= 7 6*1= 6 5*1= 5 4*1= 4 3*1= 3 2*1= 2 1*1= 1
⑤右对齐的乘法表,源代码:
i = 1
while i <= 9:
k = 1
while k <= (9 - i): #判断应该用多少个空格来填充前面的空白
print(' ',end = '') #算上空格一组式子占用7个空格,比如8*8=64 所以空7个格
k += 1

j = 1
while j <= i:
print("%d*%d=%-2d"%(j,i,i*j),end = ' ')
j += 1
print()
i += 1
执行结果:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81