Article From:https://www.cnblogs.com/jiangxuejian/p/9218746.html

The content of this section

1、Understanding the while cycle
2、whileThe use of circulation
3、Nested cycle: circulatory cycle

 

1、Understanding the while cycle

break # Terminate the current cycle

continue #Jump out of the secondary cycle

1 while Condition of judgment:#When the while satisfies the condition, the program will circulate all the time
2     Execute the statement...3 else:   #When the while normal loop is completed, the program does the else note: break will terminate the while loop, so the program does not else; and continue is skipping the secondary cycle, so the program will do else
4     Execute the statement...

 

2、whileThe use of circulation

The user enters a value and guesses the right age

 1 age = 60
 2 while True:
 3     guess_age = int(input("Age:"))
 4     if guess_age == age:
 5         print("Congratulations! Guess it!")
 6         break
 7     elif guess_age > age:
 8         print("Guess it's big!")
 9     else:
10         print("Guess little!")
11 else:
12     print("End")

 

3、Nested cycle: circulatory cycle

(1)A rectangle is output by using the horn, and the user can specify the width and height. For example, the length is 3, the height is 4, and the output is a rectangle with 3 figures and 4 vertical numbers.

 1 height = int(input("Height:"))
 2 width = int(input("Width:"))
 3 num1 = 1
 4 while num1 <= height:
 5     num2 = 1
 6     while num2 <= width:
 7         print("#",end="")
 8         num2 += 1
 9     num1 += 1
10     print()
11 else:
12     print("End")

(2)Use the while loop to enter the “99 multiplier table”

 1 num1 = 1
 2 while num1 <= 9:
 3     num2 = 1
 4     while num2 <= num1:
 5         num3 = num2 * num1
 6         print(num1, "*", num2, "=", num3, end="   ")
 7         num2 += 1
 8     print()
 9     num1 += 1
10 else:
11     print("multiplication table")

 

Leave a Reply

Your email address will not be published. Required fields are marked *