Python Basics-----While loop exercise

Source: Internet
Author: User

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

1 #!/USR/BIN/EVC Python 32 #-*-coding:utf-8-*-3 4 " "5 using while loop Output 1 2 3 4 5 6 8 96 " "7 8Count = 19  whilecount<=10:Ten     ifCount = = 7: One         Pass A     Else: -         Print(count) -Count + = 1

Second, using the while loop output 1~100 and

1 #!/USR/BIN/EVC Python 32 #-*-coding:utf-8-*-3 4 " "5 using the while loop to output 1~100 and6 " "7 8i = 19sum =0Ten  whilei<=100: Onesum = sum +I Ai + = 1 - Print(sum)

Three, using the while loop output 1~100 all the odd

# !/USR/BIN/EVC Python 3 # -*-coding:utf-8-*- " " use the while loop to output all the odd numbers within the 1~100 " "  = 0 while I <:    i+=1 while     i% 2! = 0:         Print (i)         + = 1

Iv. using the while loop to output all the even numbers within the 1~100

# !/USR/BIN/EVC Python 3 # -*-coding:utf-8-*- " " using the while loop to output all the even numbers within the 1~100 " "  = 0 while I <:    i+=1 while     I% 2 = = 0:         Print (i)         + = 1

V. Using the while loop output 1-2+3-4+5.....+99 and

1 #!/USR/BIN/EVC Python 32 #-*-coding:utf-8-*-3 4 " "5 using the while loop to output 1-2+3-4+5.....+99 and6 " "7 8i = 19sum =0Ten  whilei<=99: One     ifI% 2 = =0: Asum = Sum-I -     Else: -sum = sum +I thei + = 1 - Print(sum)

Six, determine the user login, three chance to retry

1 #!/USR/BIN/EVC Python 32 #-*-coding:utf-8-*-3 4 " "5 User Login, three chance retry6 " "7 8 9Count = 1Ten  whileCount <= 3: One  A     ifInput'Please enter user name:') =='liming': -         Print('Login Successful! ') -          Break the     Else: -         Print('Please try again! ') -Count + = 1 - Else: +     Print('has failed three times and cannot log in today. ')

Python Foundation-----while loop exercise

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.