沐风2021-while语句

[复制链接]
作者: ecithy | 时间: 2021-5-30 20:09:36 | python基础|
0 97
发表于 2021-5-30 20:09:36| 显示全部楼层 |阅读模式

while语句用于在表达式保持为真的情况下重复地执行:

count = 0
while (count < 5):
   print('The count is:', count)
   count = count + 1

print("Good bye!")

输出:

The count is: 0

The count is: 1

The count is: 2

The count is: 3

The count is: 4

Good bye!

多层循环

多个for、多个while、for+while等形式

for i in range(1, 3):
    for j in range(4, 6):
       print(i, j)

输出:

1 4

1 5

2 4

2 5

循环+判断

for+if

for i in range(1, 5):
    for j in range(2, 6):
        if i > j:
            print(i, j)

输出:

3 2

4 2

4 3

while+if

count = 0
while (count < 5):
   print('The count is:', count)
   count = count + 1
   for b in range(5):
       if count + b > 8:
           print('count + b > 8,为: ',  count + b)

print("Good bye!")

输出:

The count is: 0

The count is: 1

The count is: 2

The count is: 3

The count is: 4

count + b > 8,为: 9

Good bye!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

!jz_fbzt! 快速回复 !jz_sctz! !jz_fhlb! 按钮
快速回复 返回列表 返回顶部