Preliminary knowledge of "Python Foundation"--loop

Source: Internet
Author: User


For Loop

One, 1-100 odd

Method one: General value
for I in Range (1,101):
If I% 2==1:
Print (i)

Method Two: Using step size
for I in Range (1,101,2):
Print (i)


Two, 1-100 odd, 50-70 not printed
for I in Range (1,101):
If 50<=i <=70:
Continue
elif I 2 = = 1:
Print (i)

Third, login program, try to log in three times, success is terminated
_user= ' alon '
_passwd= ' [email& Nbsp;protected] '
for I in range (3):
User=input (' Username: ')
Passwd=input (' passwd: ')
if User==_user and PASSWD==_PASSWD:
Print (' Welcome to%s '%user)
Break
Else:
Print (' username or password is wrong, please reenter ')

while Loop
One , log in the program, try to log in three times, the success is terminated three times the hint has been tried several times. and asks if you want to continue trying
sum = 0
_user= ' alon '
_passwd= ' [email protected] '
while sum <3:
User=input (' Username: ')
Passwd=input (' [email protected]: ')
if User==_user and passwd==_passwd:
Print (' Welcome to %s '%_user)
Break
Sum + = 1
If sum ==3:
xuanze=input (' Do you want to continue? [y/n] ')
if Xuanze = = ' y ':
sum = 0

Else:
Print (' has tried several times ')

"Python Basics"--loop first knowledge

Related Article

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.