1.while Loop statement:
1 Count = 02 while Count <=:3 print(" ", Count)4 count + = 15print('-- --loop is ended----')
2. Print even numbers:
1 #Print even numbers2 #count = 03 #4 #While count <=:5 #6 #if count% 2 = = 0: #偶数7 #Print ("loop", count)8 #9 #count + = 1Ten # One # A #print ('----loop is ended----')
3.50th time do not print, 第60-80 print the corresponding value of the square
1Count =02 3 whileCount <= 100:4 5 ifCount = = 50:6 Pass #just before. 7 8 elifCount >= 60 andCount <= 80:9 Print(count*count)Ten One Else: A Print('Loop', count)
Count+=1
4. Dead Loop
1 count = 023 while True:4 print(" ", count)5count + = 1
5. Loop Termination statement: Break&continue
Break: Used to completely end a loop, jump out of the loop body and execute the statement following the loop
Continue: Does not jump out of the loop, terminates the loop, then executes the next loop, break terminates the entire loop
1 #Break Loop2 #count=03 #While count<=100:4 #print (' Loop ', count)5 #if count==5:6 # Break7 #count+=18 #print ('-----out of a while loop---')
1 #Continue cycle2Count=03 whilecount<=100:4 Print('Loop', Count)5 ifCount==5:6 Continue7Count+=18 #print ('-----out of a while loop---') #一直打印59 #continue jumps out of this loop, does not execute Count+=1,count has been 5
Example 1: Let the user guess the age
1Age = 262user_guess = Int (input ("your guess:"))3 ifUser_guess = =Age :4 Print("Congratulations on your right answer, you can hold the silly girl home! ")5 elifUser_guess <Age :6 Print("try bigger")7 Else :8 Print("try smaller")
Example 2: Users guess the age upgrade version, let the user guess age, up to guess 3 times, the middle guess on the exit loop
1Count =02Age = 263 4 whileCount < 3:5 6user_guess = Int (input ("your guess:"))7 ifUser_guess = =Age :8 Print("Congratulations on your right answer, you can hold the silly girl home! ")9 BreakTen elifUser_guess <Age : One Print("try bigger") A Else : - Print("try smaller") - theCount + = 1
Example 3: The user guess age continues to upgrade, let the user guess age, guess 3 times, if the user 3 times are wrong, ask the user whether still want to play, if y, then continue to guess 3 times, to this reciprocating "note hint: equivalent in count=3 when asked the user, if still want to play, equal to count to 0 and then run again"
Count =0age= 26 whileCount < 3: User_guess= Int (Input ("your guess:")) ifUser_guess = =Age :Print("Congratulations on your right answer, you can hold the silly girl home! ") Break elifUser_guess <Age :Print("try bigger") Else : Print("try smaller") Count+ = 1ifCount = = 3: Choice= Input ("you idiot, don't guess right, still want to continue? (y| Y)") ifChoice = ='y' orChoice = ='Y': Count= 0
The role of the else behind the 6.while&else:while is that when the while loop executes normally and the middle is not interrupted by a break, the Else statement "else detects if your loop process has been aborted (when your loop code is longer)"
1 count=0 2 # while Count <=5: 3 # count+=1 4 # print (' Loop ', count) 5 #< /span>6 # else: 7 # print (' Loop normal execution ') 8 # print (' -----out of a while loop----')
1Count=02 whileCount<=5:3 Print('Loop', Count)4 ifCount==3:5 Break6Count+=17 Else:8 Print('Loop is done')
Python--while, break, continue, else