python-Loop (for, while), Judgment, string formatting

Source: Internet
Author: User

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

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.