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