P42for loop, break statement


Loop Loop:
1. finite loop
2. infinite loop (infinite loop)


for loop:
#i is variable #range (start x, end to y, z steps of the cycle)
for I in Range (x, y, z ): # i assigned to the circulation range #range range
Print (i) # i output


example:
for i in range (2,6): Z is a step # 1 may not write default;
Print (i)
output:
> >> [2,3,4,5]

for example:
for I in Range (1, 10,): Z is a step # 2, i.e. 2 followed by progressive press
print (i)
output:
>>> [1 , 3,5,7,9]


seeking odd 1-100:
method. 1:
for I in Range (1,101):
IF I. 1% 2 ==:
Print ( "Loop:", I)
method 2:
for I in Range (1,101,2)
Print (I)

find the number 100, but in addition the number of 50-70;
for i in range(1,101):
if i<50 or i>70:
print(i,end=" ")


输入3次密码:

for i in range(3):

_user = "leng"
_passwd = "wxc"


username = input("username:")
password = input("password:")


if username == _user and password == _passwd:
print("welcome %s login %s....."%(_user,_passwd))
break;
else:
print("invalid username or password !")

进阶:

for i in range(3):
_user = "leng"
_passwd = "wxc"

panduan = False
username = input("username:")
password = input("password:")


_user username and password == IF == _passwd:
Print ( "the Login is available for purchase% S% S ....."% (_ the User, _passwd))
panduan = True
BREAK
the else:
( "! invalid username or password") Print
IF not panduan: # under false circumstances, the conditions established
print ( "test too much")

#if not: if it is false, then the output of the X-
Print (the X-)


as Advanced:

for i in the Range (3):
_user = "Leng"
_passwd = "WXC"

username = INPUT ( "username:")
password = INPUT ( "password:")

IF username and password == == _user _passwd:
Print ( "Login available for purchase% S% S .... . "% (_ user,_passwd))
after the break #break interrupt the cycle for, would not face execution last else statement
else:
( "! invalid username or password") Print
# long as the above normal for loop is finished, the middle not be interrupted, will be executed else statement
else:
Print ( "try too much")
behind only add #for else while too

Guess you like

Origin www.cnblogs.com/Black-sail/p/11544125.html