while Loop Statement of Python Learning
- 2021-12-11 18:31:14
- OfStack
The while statement is similar to the if statement
Add the required loop condition after while with ":" as the end. When the condition is met, run the program block below while until the condition behind while is no longer met. The program will jump out of the while statement and continue to run down. There are probably the following kinds:
while basic cycle
while... continue statement
while … … break statement
while … … else statement
1. while basic cycle:
If the conditions are met, the loop will start, and if the conditions are not met, it will
For example:
Show all numbers from 1 to 100
shuZi = 0 # Define variable name " shuZi "And assign it a value of 0
while shuZi < 100 : # Set the loop condition to shuZi Is less than the value of 100
shuZi = shuZi + 1 #shuZi Add the value of 1
print(shuZi) # Put shuZi The value of is displayed
'''
Display 1 To 100 A certain number
'''
2. while … … continue statement:
When running to continue, terminate the current cycle and start the next cycle
For example:
Show 1 2 3 4 5 6 8 9 10 (Don't Show 7)
shuZi = 0 # Defining variables shuZi And assigned to 0
while shuZi < 10 : # Set the loop condition to shuZi Less than 10
shuZi = shuZi + 1 #shuZi Assignment of + 1
if shuZi == 7: # Setting if Judgement condition For shuZi The value of is equal to the value of 7
continue # The following code It will no longer be executed Jump straight back while Where to start
else:
print(shuZi) # Otherwise it displays shuZi Value of
3. while … … break statement:
Terminate all loops when the program runs to break
For example:
3 logon opportunities for users
yongHuMing = 0 # Defining variables yongHuMing And assigned to 0
miMa = 0 # Defining variables miMa And assigned to 0
x = 3 # Defining variables x And assigned to 0
while x > 0 : # Set the loop condition to x Greater than 0
yongHuMing = input (' Please enter a user name: ') # Display Please enter a user name: and assign a value to yongHuMing
miMa = input (' Please enter your password: ') # Display Please enter a password: and assign a value to miMa
if yongHuMing == 'adam' and miMa == '123' : # If yongHuMing The value of is equal to the value of adam And miMa The value of is equal to the value of 123
print (' Login Successful ') # The login is successful
break # Exit loop
else:
x = x - 1 # Otherwise x Value of Minus 1
print (' Login failed. Please log in again ') # Show login failure please log in again
4. while … … else statement:
If the loop condition is no longer met, execute the command after else
For example:
Display 10 numbers from 1 to 10, and finally tell you that the display is finished
shuZi = 0 # Define variable name " shuZi "And assign it a value of 0
while shuZi < 10 : # Set the loop condition to shuZi Less than 10
shuZi = shuZi + 1 #shuZi Add the value of 1
print(shuZi) # Put shuZi The value of is displayed
else: # When shuZi Less than 10 When the conditions of cannot be met,
print(" All displays complete ") # All displays are displayed
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!