Python for loop (finite loop) and while loop (infinite loop)

 

1. for loop

# for is a finite loop, while is an infinite loop

# For the back also can be connected to the else 
_user = " zgzeng " 
_psw = " zgz " 
# restrictions landing three times, three times if the landing fails, it will automatically exit 
for i in the Range (3 ):
    username = input("Your name:")
    password = input("Your password:")
    if username == _user and password == _psw:
        print("welcome to login our system!!!")
    else:
        print("Invalid your username or password!!!")



#When logging in 3 times failed, the optimization gives a prompt and then exits 
# Method 1: Use the flag 
flag = False
 for i in range (3 ):
    username = input("Your name:")
    password = the INPUT ( " Your password: " )
     if username == _user and password == _psw:
         Print ( " is available for purchase to the Login Our System !!! " )
         # When the flag = True when the code is executed later if the 
        flag = True
         break 
    else :
         print ( " Invalid your username or password !!! " )
 if   not flag:
     print ( " Don't try it, it's been 3 times, or stupid rogue !!! " )


#Method 2: In addition to if can be followed by else, for can also be followed by 
for i in range (3 ):
    username = input("Your name:")
    password = input ( " Your password: " )
     if username == _user and password == _psw:
         print ( " welcome to login our system !!! " )
         break 
    else :
         print ( " Invalid your username or password !!! " )
 else :
     print ( " Don't try it, it's been 3 times, or stupid rogue !!! " )
 # For loop can be followed by else, but not elif 
# For loop program execution, when the program is normally executed, it will execute else. If it is not executed normally, it will not execute else. After logging in within 3 times, it will break out of the program. Here is abnormal execution and will not be executed. else program, and if the login fails within 3 times, the else program will be executed

The effect of method 1 and method 2 are the same, but obviously method 2 is more concise

 

2.while loop

If the while loop is unconditional, it is an infinite loop

while can also receive else, the same effect as for

counter = 0
while counter > 3:
    username = input("Your name:")
    password = input("Your password:")
    if username == _user and password == _psw:
        print("welcome to login our system!!!")
        # 当flag =True的时候,会执行后面if的代码
        flag = True
        break
    else:
        print("Invalid your username or password!!!")
    counter + = 1
 else :
     print ( " Don't try it, it's been 3 times, or stupid rogue !!! " )
#Program optimization every 3 trials, after 3 failures, give a selection prompt, whether to continue to try to log in 
# Idea: After every 3 trials in the loop, the program will give a choice, then we need to try after 3 , Clear the 

counter_user2 = " zgzeng " 
_pwd = " zgzeng "  
counter = 0
 while counter <3 :
    username = input ( " Please enter your username: " )
    password = input ( " Please enter a password: " )
     if _user2 == username and password == _pwd: 
         print ( " Welcome to log in to my program " )
         break 
    else :
         print ( " Your username or password is incorrect " )
    counter + = 1 
    keep_going_choice = input ( " Enter [y] will continue, others exit " )
     if keep_going_choice == " y " :
        counter = 0
 # implementation of the results here are each performed once, the program will ask whether to continue, and did not realize the effect our ideal (3 times asking once)

Need to add another judgment

_user2 = "zgzeng"
_pwd = "zgzeng"
counter = 0
while counter < 3:
    username = input ( " Please enter your username: " )
    password = input ( " Please enter a password: " )
     if _user2 == username and password == _pwd:
         print ( " Welcome to log in to my program " )
         break 
    else :
         print ( " Your username or password is incorrect " )
    counter + =. 1
     IF counter ==. 3:   # plus a determination 
        keep_going_choice = INPUT ( " input [y] will continue, else quit " )
         IF keep_going_choice == " Y " :
            counter = 0

 

# For limited circulation, while infinite loop 
# for the back also can be connected to the else _user = "zgzeng" _psw = "zgz" # landing restrictions three times, three times if the landing fails, it will automatically exit for i in the Range ( 3 ): username = input ( "Your name:" ) password = input ( "Your password:" ) if username == _user and password == _psw: print ( "welcome to login our system !!!" ) else : print ( "Invalid your username or password !!!" ) # When the login fails 3 times, the optimization gives a prompt and then exits















# 方法1:使用标记
flag = False
for i in range(3):
username = input("Your name:")
password = input("Your password:")
if username == _user and password == _psw:
print("welcome to login our system!!!")
# 当flag =True的时候,会执行后面if的代码
flag = True
break
else:
print("Invalid your username or password!!!")
if not flag:
print( "Don't try it, it's been 3 times or stink rogue !!!" )


# Method 2: In addition to if can be followed by else, for can also be followed by
for i in range ( 3 ):
username = input ( "Your name: " )
password = input ( " Your password: " )
if username == _user and password == _psw:
print ( " welcome to login our system !!! " )
break
else :
print ( " Invalid your username or password! !! " )
else :
print ( " Don't try it, it's been 3 times, or stupid rogue !!! ")
# for loop can be followed by else, but not elif
# for loop program execution, when the program is normally executed, it will execute else, if it is not executed normally, it will not be executed, after landing here within 3 times, it will break Jump out of the program, here is the abnormal execution, the else program will not be executed, and if the login fails within 3 times, the else program will be executed

Guess you like

Origin www.cnblogs.com/zgzeng/p/12748246.html