Python's for,while and if

Source: Internet
Author: User

Python's loop and Judge authentication account and password
ImportGetpass#Import Moduleusername= Input ('Username:') Password= Getpass.getpass ('Password:')#Enter password not displayedUser Password Authentication: if and Else#Author:yao XiaodongImportGetpass_username=' Arthur'_password='123456'username= Input ('Username:') Password= Input ('Password:') if_username = = Username and_password = =Password:Print('Welcome {name} to Python world'. Format (name=username))Else:    Print('Auth filed')
Guessing age while way
#While LoopAuthur_age = 500Count=0 whileCount < 3: Guess_age= Int (Input ('Guess_age:'))    ifGuess_age = =Authur_age:Print('Yes,you is right!')         Break    elifGuess_age >Authur_age:Print('It 's to bigger ...')    Else:        Print('It 's to smaller ...') Count+ = 1Else:    Print(' Error')
For way
For Loop Authur_age= 500 forIinchRange (3): Guess_age= Int (Input ('Guess_age:'))    ifGuess_age = =Yxd_age:Print('Yes,you is right!')         Break    elifGuess_age >Yxd_age:Print('It 's to bigger ...')    Else:        Print('It 's to smaller ...')Else:    Print(' Error')
While+if judge whether to continue after three times
Age = 21Count=0 whileCount < 3: Guess_count= Int (Input ("Guess_count:"))    ifGuess_count = =Age :Print('Yes')
Break
elifGuess_count >Age :Print('Big') Else: Print('Small') Count+ = 1ifCount = = 3: Paly= Input ('Do you want paly Agen? (y/n):') ifPaly = ='y': Count=0elifPaly = ='N': Print(' All right')
Else: Print('(y/n) Do you know?')

What is the difference between break and continue?

Different points

Break jumps out of the loop, and if a break is encountered in the loop, the loop is directly invalid.

Continue jumps out of the current loop, if the loop encounters a continue, jump out of the loop to continue the next loop.

Same point:

Break and continue all only jump out of the current loop, that is, if the for loop is a for loop, it will only jump out of the for loop where the break or continue is located. As an example:

For example:

 forIinchRange (3):    Print('---------'I'-------')     forNinchRange (3):        ifn >1:Print(n)Else:             Break#run the result as---------0----------------1----------------2-------
#也就是说遇到了break后整个循环都没有了结果, but the outer loop has no effect

Look at continue again.

 forIinchRange (3):    Print('---------'I'-------')     forNinchRange (3):        ifn > 1:            Print(n)Else:            Continue#run the result as---------0-------2---------1-------2---------2-------2
#也就是说continue只是跳出一次循环, not the whole cycle,
But the outer loop has no effect.

Python's for,while and if

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.