Python 4 Loop statement while

Source: Internet
Author: User

While [condition]: Here the condition satisfies the Boolean operation true to infinite loop while inside the code.

The basic while loop of the fixed condition,

If the if match then the print login succeeds, and the break jumps out of the entire loop, otherwise the else input is executed and re-cycled.

While 1==1


if user = = "Admin" and pwd = = "PWD"
Print ("Login Successful")
Break
Else
Print ("User name or password error")
Digital output Cycle 1: Outputs 1, 2, 3, 4, 5, 6, 8, 9, 10. And break jumps out of all loops.
Shuchu = 1 while true:              a while loop is set, and the condition is always true. The results are written dead all the time.    if Shuchu = = 7:        pass                    when condition satisfies shuchu=elif Shuchu = = one break else Print# Add 1 to the original Shuchu and start the next loop. 

Note: If there are multiple if in the same level in the code above and the condition matches, of course this is wrong, then the code will only perform the last

The code in an If. The conclusion of the test in Python3.5.

Digital output Cycle 2: Outputs 1, 2, 3, 4, 5, 6, 8, 9, 10. And break jumps out of all loops.
Shuchu = 1while True:    if Shuchu = = 7: "" "          when Shuchu = 7 executes a Shuchu plus 1 action, and then immediately returns to the while again
The benefit of starting execution of the entire code, compared to the above, eliminates the subsequent code time. "" " Shuchu = 1 + Shuchu if Shuchu = = Allelseprint(shuchu) Shuchu = 1 + shuchu

Digital output Loop 3: Same as above, but the exit termination mechanism is set through the while condition.

Cyclic calculation and output: Calculates the sum of 1-100 numbers:
Calculation logic: We need to add 1 2 3 4 5 and so on, we'll take a value of 1 and start the calculation for the first time we take out the number 1 and put it in the pool.
The second time we take out the number 2, add it to the 1 in the pool. The third time we take out the number 3 and add it to the 2 in the pool. The pool is always forward.
The number of all occurrences of a polygon. This is until after the 100 this number appears. We have 1-100 of the numbers appearing and summing up. and value
Is our pool, and count is the number of times we start from 1 to +1.
Initial loop Start value = 0 Count = 0 Second cycle start value = 1 count = 2 Value = value +count = 1 + 2 third cycle start V Alue = 3 count = 3 Value = value +count = 3 + 3 Fourth cycle start value = 6 Count = 4 Value = value +c Ount = 6 + 4 The fifth cycle begins with value = Ten count = 5 Value = value +count = ten + 5

The code is as follows:

Value = 0            # is used to store the result of each calculation count = 0            # used for calculation count while count < 101: #当计数在100以内都执行下面代码 value = Value + count #value的值每次都增加一个count值. Count = count + 1 #count每循环一次基数 +1 print(value) #The while loop finishes printing out the value of value. 

Exercises:

1 while loop output 1 2 3 4 5 6 8 9 10

2 asks 1-100 of the and of all natural numbers.

3 out of 1-100 all odd numbers use the condition of the remainder condition code seat if, for example, if variable%2 = = 1: This means that it is an odd number

4 out of 1-100 all even use the conditions for the remainder condition code seat if condition such as if variable%2! = 1: This means an even number

5 1-2+3-4+5...99 of all numbers

6 User Login 3 chance.

Answers to the exercises

Answers to 3 questions

Value = 0count = 1 whilecount <:    if count% 2 = = 1:     # Odd add value + =# even minus Value -= count Count + = 1print (value)         

Python 4 Loop statement while

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.