The content of this section
1. Recognize while loop
2. The use of the while loop
3. Nested loop: Loop sleeve loop
1. Recognize while loop
Break # terminates the current loop
Continue #跳出当次循环
1 While judging condition: # when the while satisfies the condition, the program will always loop 2 execution Statement ... 3 else: # when the normal loop completes, the program does the else note: break terminates the while loop, so the program does not do else , and continue is skipping cycles, so the program will execute the else4 Execution statement ...
2. The use of the while loop
The user enters a value to guess the correct age
1Age = 602 whileTrue:3guess_age = Int (input ("Age:"))4 ifGuess_age = =Age :5 Print("Congratulations to you! Guess right! ")6 Break7 elifGuess_age >Age :8 Print("guess it's big! ")9 Else:Ten Print("guess it's small! ") One Else: A Print("End")
3. Nested loop: Loop sleeve loop
(1) using the # number to output a rectangle, the user can specify width and height. Columns such as: Length 3, height is 4, output a horizontal with 3 # number, vertical with 4 # Number of rectangles
1height = Int (input ("Height:"))2width = Int (input ("Width:"))3NUM1 = 14 whileNUM1 <=Height:5num2 = 16 whileNum2 <=Width:7 Print("#", end="")8Num2 + = 19NUM1 + = 1Ten Print() One Else: A Print("End")
(2) Input "99 multiplication table" with while loop
1NUM1 = 12 whileNUM1 <= 9:3num2 = 14 whileNum2 <=NUM1:5NUM3 = num2 *NUM16 Print(NUM1,"*", Num2,"=", num3, end=" ")7Num2 + = 18 Print()9NUM1 + = 1Ten Else: One Print("99 multiplication Table")
Getting started with Python-recognize while loops and use