Python for Loop
ImportRandomrandom_num=random.randint (1,1000)Print(random_num); forIinchRange (3): Num=int (Input ('Please enter')) ifNum>Random_num:Print('it's too big.') elifnum<Random_num:Print('it's too small.') Else: Print('guess right.')
While loop
# loop, iterate, traverse # For Loop # while # The loop is a repetition of what you do. # Specify a loop end condition # with a while loop, you have to have a counter # continue end this cycle and proceed to the next cycle # Break End Loop
Count=0#counter whileCount<3: Username=input ('Please enter your username:') PWD=input ('Please enter your pwd:') ifusername=='Nhy' andpwd=='123456': Print('Welcome to visit') Break Else: Print('account/Password Error! ') Count+=1Else: Print('excessive number of errors')
Judge
Score=input ('Please enter your score:')#input received is all str type#int coercion type conversionScore=Int (score)ifScore<60: Print('inferior lattice') ifScore>50: Print('hahaha') elifScore<50: Print('Little Fool .') Else: Print('-----')elifScore>=60 andScore<80: Print('Pass')elifScore>=80 andScore<90: Print('Good')Else: Print('Excellent')
String
forIinchRange (5): Username=input ('Please enter your name:') Time='December 17, 2017 17:30' Print(username+', welcome,'+'time is:'+time)#stitching two strings with a plus sign Print('%s, welcome, time is:%s'%(username,time))Print( '{name}, welcome, time is: {date}, tomorrow's time is {date}'. Format (name=username,date=Time ))
python-Loop (for, while), Judgment, string formatting