python basis: while loop

while loop

When entering the loop condition is satisfied, enter the circulation, when the condition is not satisfied, out of the loop

Example 1:

i =0
while i < 3:
i+=1
print(i)

After the results:

 

Python language allows a circulation loop embedded inside another

Example 2:

i,j =1,10
while i < 20:
while i <j:
j-=5
i+=5
print (i,j)

After the results:

 

The following cases combined with a better understanding

Case 1:

5 apple, pear 4, 3 watermelon, cantaloupe 6, three peach, orange 7

While loop to find the peaches from the above, it counts the number of peaches, and judgment is odd or even

 

 

fruits = '5 apples, pears 4, 3 watermelon, cantaloupe 6, three peach, orange 7' 
I = 0
fruits_len = len (Fruits)
the while I <fruits_len:
IF Fruits [I: I + 2 ] == 'peach':
Print ( "peach in that position:" + fruits [i-6 : i-3] + " and" + fruits [i + 5: i + 7] + " between")
IF int (Fruits [I + 2])% 2 == 0:
Print ( "total number of peaches% d, the even-numbered"% int (Fruits [I + 2]))
the else:
Print ( "total number of peaches% d, odd "% int (Fruits [I + 2]))
I + =. 1

After the results:

 

Case 2: The user interface Log

1. Enter the user name and password

2. determine the user name and password are correct

3. After three failed login error

 

. 3 = I 
the while 0 <I <=. 3:
User = INPUT ( 'Please enter your account number:')
password = INPUT ( 'Enter your password:')
IF User == 'ADMIN' and password == '123456 ':
Print ( "Login successful")
BREAK
the else:
Print ( "Login failed chance you have% d"% int (i-1))
i - = 1
the else:
Print ( "more than three times the number of login, please wait try again ")

 

Operating results Results:

login successful

 

Login failed

 

Guess you like

Origin www.cnblogs.com/my-essay/p/11078215.html