A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 梦缠绕的时候 黑马粉丝团   /  2019-1-16 10:46  /  1210 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一。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(起始,结束,步长)
取头不取尾


1 个回复

倒序浏览
奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马