Section Tenth, while loop

Source: Internet
Author: User

While loop

While loop, is a combination of loop plus judgment, satisfies the judgment condition return true (True) starts loop code block, does not satisfy the judgment condition returns false () does not loop

Format:

While condition:

code block

Note: In the while loop, if the judgment or keyword is not terminated, the loop will be looped (dead loop)

Such as:

 1  #   2  #  -*-coding:utf-8-*- 3  import  Time #   reference timer module  4  n = True  5  s = 16    n:  7  print    8  time.sleep (1) #   call timer  

As shown above while the loop evaluates the variable n equals True (True) when the loop starts looping inside the code block, printing the variable s, encountering the timer waiting 1 seconds after the process is repeated (dead loop)

The combined use of the while loop and if judgment, from 1 cycles to 10 termination

  Such as:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportTime#Reference Timer Module4n =True5s = 16  whileN:7     Print(s)8     ifs = = 10:9n =FalseTenS+=1#variable s self-accumulating plus 1 OneTime.sleep (1)#Call Timer

The above is from 1 loop to 10 termination, while loop to determine the n variable is true (True), start loop code block, print s variable value is 1, down encountered if the value of s variable is equal to 10, obviously not equal, not equal to the judgment, in the down encounter s+=1 (s variable itself value plus 1, Equal to s=s+1), at this time the value of the s variable is 2, in the Down encounter timer wait 1 seconds, and then start repeating the loop again until the s variable is summed to the 10,if to determine that the S variable equals 10, execute if the inside of the code block, if the code block inside is n variable re-assignment equals False (False) , the n variable is already false (false), and in the secondary loop, the while loop condition determines that the n variable is false, not executing while the loop terminates

While Loop keyword (break) jumps out of the entire loop not looping

  such as: Cycle out 1 to 10

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportTime#Reference Timer Module4n =True5s = 16  whileN:7     Print(s)8     ifs = = 10:9          BreakTenS+=1#variable s self-accumulating plus 1 OneTime.sleep (1)#Call Timer

Above, when looping to 10 o'clock, if the s variable equals 10, execute if the code block encounters a break (jumping out of the loop is not the loop) and it jumps out and is not in the loop.

While Loop keyword (continue) jump out of this cycle to continue the next loop

  such as: Cycle out 1, 2, 3, 4, 6, 7, 8, 9, 10, jump out of 5

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportTime#Reference Timer Module4n =True5s = 16  whileN:7     ifs = = 5:8s + = 19         ContinueTen     Print(s) One     ifs = = 10: A          Break -s + = 1#variable s self-accumulating plus 1 -Time.sleep (1)#Call Timer

As listed above, when the loop to 5 when the If judgment, execute if inside the code block, if the s variable plus 1 is 6,continue jump out of this cycle, again the s variable is 6, if judgment equals 5 does not set up skip judgment, down execution, Until the second if judgment equals 10 after the break jumps out of the entire loop not in the loop

While Loop 1-100 sum of sums (1+2+3+4+5...+100)

  Such as:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3n =True4s = 15t =06  whileN:7     Print(s)8t = t +s9     ifs = = 100:Ten          Break Ones + = 1#variable s self-accumulating plus 1 A Print(t)

As shown above, T = t + S is each time the loop is over here, the value of the s variable itself plus 1 in addition to the value of the T variable is re-assigned to the T variable (it can be understood that the s variable each loop itself accumulate plus 1 value, in addition to the value of the T variable, re-assign to the T variable to do The last registration of the T variable is the sum of the entire loop and

While Loop plus if judgment all odd numbers within the output 1-100 

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3s = 14  whileS <= 100:5t = s% 2#let the value of the s variable be re-assigned to the T variable after remainder6     #s equals 0 is even, equals 1 is odd7     ift = = 1:8         Print(s)9     Else:Ten         Pass Ones + = 1#variable s self-accumulating plus 1

That is, to cycle out 1-100, and then in the loop to define a variable t, each loop to the value of the s variable to be assigned to the T variable, and then determine the value of the T variable, if 1 is an odd number to print out the s variable, if not 1 is even if you do not print pass, so that the way to cycle plus judgment Print out the odd number within 1-100.

While Loop plus if judgment all even numbers in the output 1-100 

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3s = 14  whileS <= 100:5t = s% 2#let the value of the s variable be re-assigned to the T variable after remainder6     #s equals 0 is even, equals 1 is odd7     ift = =0:8         Print(s)9     Else:Ten         Pass Ones + = 1#variable s self-accumulating plus 1

That is, to cycle out 1-100, and then in the loop to define a variable t, each loop to the value of the s variable to be assigned to the T variable, and then determine the value of the T variable, if 0 is even print out the s variable, if not 0 is an odd number will not print pass, so that the way of the Loop plus judgment, Print out the odd number within 1-100.

While Loop plus if judgment all the numbers of 1-2+3-4+5...99 and

  Such as:

1#!/usr/bin/env python2#-*-coding:utf-8-*-3R =04s =15  whileS <= About:6t = s%27     ift = =1:8R = r +s9     Else:TenR = R-s Ones + =1 APrint (R)

That is, in the loop to make the judgment, the loop to the odd number of the addition, if it is even to subtract, each time the addition or subtraction of the loop, the assignment to the R variable record, r variable the last record is we get the addition and subtraction and

While loop plus if the user is given a 3 chance to enter the user name and password

Such as:

1#!/usr/bin/env python2#-*-coding:utf-8-*-3A =04  whileA <3:5YH = input ("Please enter user name")6MM = input ("Please enter your password")7     ifYH = ="Guixiu"and mm = ="123":8Print"Congratulations on your success .")9          BreakTen     Else: OnePrint"Unfortunately, the password or user name is wrong") AA + =1

That is, using the while loop plus if judgment, in the loop to determine whether the user entered the user name and password is correct, the correct login successfully exited the entire loop, incorrect prompt error, variable a plus 1, repeat the operation again, when the value of variable A is greater than 3 o'clock, the loop condition is not established, stop the loop

  

  

Section Tenth, while loop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.