PythonWhile Loop statement
In Python programming, a while statement is used to loop the execution of a program, which, under certain conditions, loops through a program to handle the same tasks that require repeated processing. Its basic form is:
While judging condition: Execute statement ...
The execution statement can be a single statement or a block of statements. The judging condition can be any expression, and any value other than 0, or non-null (NULL), is true.
The loop ends when the condition false false is judged.
The execution flowchart is as follows:
While statement there are two other important command Continue,break to skip the loop, continue used to skip the cycle, break is used to exit the loop, in addition, "judging condition" can also be a constant value, indicating that the loop must be set up, the following:
1 #Coding=utf-82 " "3 @author: Nelson.huang4 @Date: 2017.07,255 @Funtion: Exercises for while loops:6 The user enters a number less than 1000, outputs all results, and stops at the user input results here, and asks the user whether to continue the loop. 7 If the user enters a string other than ' N ', continue looping and ask the user again to enter a stop number to continue. 8 9 " "Ten OnePrint_num = input ('Please input the count:') ACount =0 - whileCount < 1000: - ifCount = =Print_num: the Print "there you got the number:", Count -Choice = Raw_input ('Do I want to continue to loop? (y/n):') - ifChoice = ='N': - Break + Else: -Print_num = input ('Please re-input the count:') + A ifPrint_num <Count: at Print 'your num is passed, please re-input:' -Print_num = input ('Please input the count:') - Continue - - Else: - Print 'Loop:', Count in -Count +=1
Execution Result:
C:\Python27\python.exe c:/work/python/while.py
Please input the Count:5
loop:0
Loop:1
Loop:2
Loop:3
Loop:4
There you got the Number:5
Do I want to continue to loop? (y/n): Y
Please re-input the Count:8
Loop:5
Loop:6
Loop:7
There you got the Number:8
Do I want to continue to loop? (y/n): Y
Please re-input the Count:4
Your num is passed, please re-input:
Please input the count:16
Loop:8
Loop:9
Loop:10
Loop:11
Loop:12
Loop:13
Loop:14
Loop:15
There you got the Number:16
Do I want to continue to loop? (y/n):
Python Learning while loop