温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

条件控制之while和for

发布时间:2020-07-28 11:33:04 来源:网络 阅读:144 作者:okada88 栏目:编程语言

一。while 循环
     1.循环:重复做某件事

    2.语法
         while  条件:
         code1

    3.结束while的方式:
         1.条件不满足,下次循环开始时判断
         2.break直接结束本层循环

    4.while + continue
         continue 之后的代码不会运行了,直接开始下次循环
n= 0
while n < 6:
     if n == 4:
         n+=1
         continue
     else:
         print(n)
     n+=1

       
     5.while循环嵌套
         while:
             while:
                 while:
        

        break
             break
                 break
                
                
                
                
                
count = 1
tag = True
while tag:
     name = input("name: ")
     passwd = input("passwd ")
     if count == 3:
         print("too many")
         break
     if name == "chad" and passwd == "123":
         print("successfull")
         while tag:
             print("""
             1
             2
             3
             """)
             cho = input("choice:")
             if cho == "1":
                 print(1)
             elif cho == "2":
                 print(2)
             else:
                 print(3)
                 tag = False
     else:
         print("error")
         count +=1
                
    
    
     6. while + else
         如果while循环没有被break打断,才会执行
         循环要正常结束
count = 0
while count < 3:
     print(count)
     count += 1
else:
     print("run")   

二。for   循环
     循环取值简洁
         for+brek
         for+continue
         for+else

    dic = {"name":"sdfa","age":23}
     for i in dic:
         print(i,dic[i])
     返回字符串

    dic = {"name":"sdfa","age":23}
     for i,r in dic.items():
         print(i,r)
     以字符串返回key和value  

    dic = {"name":"sdfa","age":23}
     for i in dic.items():
         print(i)
     以元组返回key和value,一对key,value是一个元组

range(起始,结束,步长)
取头不取尾

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI