Python basis of 1: Cycle

while loop

basic structure

while conditions: 
    loop the else :  Code fast

Simple cycle

 

while True:
    print('1')
    print('2')
    print('3')
    print('4')

 

Stop the cycle

 

1 . The conditions change-circulating 
    In Flag = True
     the while In Flag:
         Print ( ' 1 ' )
         Print ( ' 2 ' )
         Print ( ' . 3 ' ) 
        In Flag = False
         Print ( ' . 4 ' )
 2. BREAK : circulating out
     the while In Flag:
         Print ( ' . 1 ' )
         Print ( ' 2 ')
         Print ( ' . 3 ' )
      BREAK 
        Print ( ' . 4 ' )
 3. Continue , out of this cycle, the next cycle continues
     the while In Flag:
         Print ( ' . 1 ' )
         Print ( ' 2 ' )
         Print ( ' . 3 ' )
      Continue 
        Print ( ' . 4 ' )    
 4. the while else loop: If the cycle is interrupted break, is not performed else 
    count= 1
    while count < 5:
        print(count)
        if count == 2:
            break
        count = count + 1
    else:
        print(666)

 

 

 

 

for loop

basic structure

 

Finite loop:
     for variable iterable; 
        statements 
may also use BREAK / contiune
 IF  else and while usage else like

 

Example:

 

First, the outputs odd 1-100

1 for i in range(1,101):
2     if i % 2 == 0: 3 continue 4 else: 5 print('loop:',i)
1 for i in range(1,101):
2     if i % 2 == 1: 3 print('loop:',i)
1 for i in range(1,101,2):
2     print('loop:',i)

 Second, the output Masterpieces 1 to 100, and 50 to 70 does not output

1 for i in range(1,101):
2     if i % 2 == 0: 3 continue 4 elif i >=50 and i <= 70: 5 continue 6 else: 7 print('loop:',i)
1 for i in range(1,101):
2     if i > 70 or i < 50: 3 print('loop:',i)

Third, a mimic account login program, out of three errors

 1 user = 'CatdeXin'
 2 passwd = 'abc123'  3  4 passwd_authentication = False  5  6 for i in range(3):  7 username = input('username: ')  8 password = input('password: ')  9 10 if username == user and password == passwd: 11 print("welcome %s login..."% user) 12 passwd_authentication = True 13 break 14 else: 15 print("Invalid username or password !") 16 17 if not passwd_authentication: 18 print('Youve tried too many times')
= User. 1 ' CatdeXin ' 2 the passwd = ' abc123 ' . 3 . 4 for I in Range (. 3 ): . 5 username = INPUT ( ' username: ' ) . 6 = INPUT password ( ' password: ' ) . 7 . 8 IF == User username and == password the passwd: . 9 Print ( " available for purchase Login% S ... "% User) 10 BREAK # after break for, will not be executed later else statement . 11 else : 12 is Print ( "Username or password Invalid! " ) 13 is else: # long as the above for loop is finished, the intermediate is not interrupted, executes else statement 14 Print ( ' Youve Tried TOO MANY Times ')

 Fourth, double jump

 1 exit_flag = False
 2 
 3 for i in range(10):  4 if i < 5:  5 continue #跳出当次循环  6 print(i)  7 for j in range(10):  8 print('Tow level:',j)  9 if j == 6: 10 exit_flag = True  #you jump 11 break 12 if exit_flag == True:  #i jump 13 break #双层跳出

 

Guess you like

Origin www.cnblogs.com/shangqiu/p/11234586.html