Python Learning Path (7)--while cycle

Source: Internet
Author: User

While loop

  

1. Basic cycle

123456 while条件:         # 循环体    # 如果条件为真,那么循环体则执行    # 如果条件为假,那么循环体不执行

2. Break

Break to exit all loops

1234 whileTrue:    print "123"    break    print"456"

3, continue

Continue is used to exit the current loop and continue the next loop

1234 whileTrue:    print "123"    continue    print"456"
Exercises

1. Use while loop input 1 2 3 4 5 6 8 9 10

2. For all numbers of 1-100

3. All odd numbers in output 1-100

4. All even numbers in output 1-100

5, Beg 1-2+3-4+5 ... 99 of all numbers of the and

6. User Login (three chance retry)

1. Use while loop input 1 2 3 4 5 6 8 9 10
n = 1
While N < 11:
if n = = 7:
Pass
Else
Print (n)
n = n + 1

Print ('----end----')

2. For all numbers of 1-100
n = 1
s = 0
While N < 101:
s = s + N
n = n + 1

Print (s)

3. All odd numbers in output 1-100

n = 1
While N < 101:
TEMP = n 2
If temp = = 0:
Pass
Else
Print (n)
n = n + 1

Print ('----end----')

4. All even numbers in output 1-100

n = 1
While N < 101:
TEMP = n 2
If temp = = 0:
Print (n)
Else
Pass
n = n + 1

Print ('----end----')

5, Beg 1-2+3-4+5 ... 99 of all numbers of the and
n = 1
s = 0 # s is the sum of all previous numbers
While n < 100:
TEMP = n 2
If temp = = 0:
s = N-S
Else
s = s + N
n = n + 1

Print (s)

Python Learning Path (7)--while loop

Related Article

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.