While Loop statement:
The while statement is used to loop the execution of a program that, under certain conditions, loops through a program to handle the same tasks that require repeated processing.
The format is:
While condition judgment:
The executed statement
Example: Guessing a number game, defining a number, and guessing within three times
Luck_num = 56num2=-1Guess_count=0 whileGuess_count < 3: num2= Int (Input ("Please input your num:")) ifnum2 >Luck_num:Print("your num is too big") elifNum2 <Luck_num:Print("your num is too little") Else: Print("congratulation your num is correct") BreakGuess_count+ = 1#the above loop condition does not satisfy the time to execute else. But if the loop above does not exit normally, the else code does not executeElse: Print("too many times you try")
The while statement has two other important commands, Continue,break, to skip the loop, continue to skip the loop, and break to exit the loop.
Python base 5 While loop statement