P42for循环,break语句

馋奶兔 提交于 2019-11-30 01:34:09
循环loop:         1.有限循环         2.无限循环(死循环)                  for循环:    #i是变量           #range(开始为x,结束为y,步长为z的循环)        for i in range(x,y,z):  #把range循环赋给i      #range范围               print(i)  #输出i例如:       for i in range(2,6):  #步长Z为1可以默认不写;           print(i)  输出为:       >>>[2,3,4,5]例如:       for i in range(1,10,2): #步长Z为2,即按2依次递进           print(i)输出为:       >>>[1,3,5,7,9]求1-100的奇数:方法1:     for i in range(1,101):        if i % 2 == 1:            print("loop:",i)方法2:    for i in range(1,101,2)        print(i)求100的数,但除了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:")            if username == _user and password == _passwd:            print("welcome %s login %s....."%(_user,_passwd))            panduan = True            break        else:            print("invalid username or password !")if not panduan:   #假的情况下,条件成立    print("试太多了")    #if not:如果是假的,则输出X    print(X)一样进阶: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   #break打断循环  for过后,就不会执行最后面的else语句        else:            print("invalid username or password !")#只要上面的for循环正常执行完毕,中间没被打断,就会执行else语句else:    print("试太多了")#for后面只能加else while也一样      
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!