Python while loop if elif else judgment

Source: Internet
Author: User

1. While loop

Contact Code: M=0#defines the value of m as 0Print('before the cycle begins m=%s'%m) whileRange (10): M+=1#value of +1 per cycle m    Print("The first%s loop, at which time m=%s"%(M, m))ifm%2==0:Print(m)ifM >=10:        Print('begin to Judge M, at this time m=%s'%m) Break            

Operation Result:

The loop starts beforethe m= 0 1th loop, atwhich time m =1 2nd Loop, at which time M =22 3rd loop, M =3 4th Loop, at this time m =44 5th Loop, atthis time m = 5 6th Loop, at this time m=66 7th Loop, atthis time m =7 8th Loop, atthis time m =88 9th Loop, at this time m =9 10th loop, at this time m= 1010 began to Judge M, at this time m=10

2. If Else judgment

Simple example: Boy_age= 32Count=0 whileTrue:count+ = 1Oldboy_age= Input ('Oldboy_age:')    ifOldboy_age:oldboy_age=Int (oldboy_age)Else:        ifCount = = 3:            Print('Sorry,%s times exhausted, please rerun'%count) Break        Else:            Print('you still have the%s chance .'% (3-count)) Continue    ifOldboy_age = =Boy_age:Print('Yes, the answer is right.')         Break    elifOldboy_age >Boy_age:Print('Take a little guess ...')    Else:        Print('go big and guess ....')    ifCount = = 3: S= Input ('would you like to re-guess ...? (y/n)')        ifs! ='N': Count=0 Break

This code is used as a test learning, there is an imperfect situation, you can copy the code to run the test locally.

s = 5
S1 = Int (input (' A: '))
If S1 > 5:
Print (' No,is small ')
Elif S1 < 5:
Print (' No,is big ')
Else
Print (' Yes, bingo ')
a lot of detailed judgment, if elif Else

3. Keyword explanation

 Break    # return, exit the entire loop Continue    # return, exit when secondary loop

Python while loop if elif else judgment

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.