Introduction to Python Programming (021) - Loop Structure Programming (2): while loop

Introduction to Python Programming (021) - Loop Structure Programming (2): while loop

1. Syntax of while loop

The while loop uses a condition to control whether to continue executing the statements in the loop body. Its syntax format is as follows:

while 条件表达式:
    循环体

illustrate:

(1) When the return value of the conditional expression is True, execute the statements in the loop body;

(2) After the execution of the loop body is completed, the return value of the conditional expression is re-judged until the result returned by the expression is False and the loop exits.

For example:

num = 10
while num != 0:
    num = int(input("请输入一个整数:"))
    print("你输入的整数为:",num)
    
程序运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个整数:22
你输入的整数为: 22
请输入一个整数:1
你输入的整数为: 1
请输入一个整数:-22
你输入的整数为: -22
请输入一个整数:0
你输入的整数为: 0

2. while True loop (the condition of the while loop is always true)

The format of the while True loop is as follows:

while True:
    循环体

For example:

(1) Display the current time and refresh it every 1 second

import time
while True:
    print(time.strftime("%Y-%m-%d  %H:%M:%S",time.localtime()),end="\r")
    time.sleep(1)

The results of the above program are as follows:

Insert image description here

(2) Display the shift schedule

import time
list1 = ["张三","李四","王强","张静","刘鹏","王菲","刘莉"]
while True:
    x = list1.pop(0)
    list1.append(x)
    print("当前值班人员名单:",list1[0],end="\r")
    time.sleep(1)

The results of the above program are as follows:

Insert image description here

(3) Use break to exit the while True loop

num = 0
while True:
    num = int(input("请输入一个数:"))
    if num == 0:
        print("输入的是0,循环结束。")
        break
            
程序运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:10
请输入一个数:-25
请输入一个数:1
请输入一个数:2
请输入一个数:0
输入的是0,循环结束。

3. Use the break statement in the loop body

Use the break statement to terminate the current loop. The break statement can be used in both for loops and while loops. The syntax of the break statement is relatively simple. You only need to add it to the loop body of a while or for loop.

The break statement is generally used in conjunction with the if statement to indicate breaking out of the loop under certain conditions. If you use nested loops, the break statement will break out of the innermost loop. The form of using break statement in while loop is as follows:

while 条件表达式1:
    循环代码
    if 条件表达式2:
        break

The form of using break statement in for loop is as follows:

for 迭代变量 in 对象:
    循环代码
    if 条件表达式:
        break

For example:

(1) Determine whether an integer is a prime number

Use a for loop:

num = 3
for i in range(2,int(num ** 0.5) + 1):
    if num % i == 0:
        print(num,"不是素数")
        break
else:
    print(num,"是素数")
                
程序运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
3 是素数

Use a while loop:

num = 7
i = 2
while i < int(num ** 0.5) + 1:
    if num % i == 0:
        print(num,"不是素数")
        break
    i += 1
if i >= int(num ** 0.5) + 1:
    print(num,"是素数")     
    
程序运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
7 是素数

(2) Accumulate the input numbers (ends when 0 or a negative number is entered)

sum1 = 0
while True:
    s = float(input("请输入数值:"))
    if s <= 0:
        break
    sum1 += s
    print("累加和为:",sum1)
        
程序运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入数值:2
累加和为: 2.0
请输入数值:2.5
累加和为: 4.5
请输入数值:12.6
累加和为: 17.1
请输入数值:0

4. Use the continue statement in the loop body

The continue statement can terminate this loop and enter the next loop in advance. The syntax of the continue statement is relatively simple. You only need to add it to the loop body of the corresponding for loop or while loop.

The form of using the continue statement in a while loop is as follows:

while 条件表达式1:
    循环代码
    if 条件表达式2:
        continue

The form of using the continue statement in a for loop is as follows:

for 迭代变量 in 对象:
    循环代码
    if 条件表达式:
        continue

For example: perform an accumulation operation on the input numbers (ignoring negative numbers), and enter 0 to end

sum1 = 0
while True:
    s = float(input("请输入数值:"))
    if s < 0:
        continue
    sum1 += s
    print("累加和为:",sum1)
    if s == 0:
        break
                
程序运行结果为:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入数值:12
累加和为: 12.0
请输入数值:4
累加和为: 16.0
请输入数值:5.2
累加和为: 21.2
请输入数值:-233
请输入数值:-52
请输入数值:0
累加和为: 21.2

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/132309985
Recommended