• while循环,for循环


     一、while循环

     1、语法:

    while 条件:
    代码1
    代码2
    代码3
    ...
    2、基本使用
    ps:for循环能做的while循环都能做,但for循环能遍历值使用比较方便
    使用(1)
    print('start.....')
    while 10 < 3:
        print('hello1')
        print('hello2')
        print('hello3')
    
    print('end....')

    使用(2)

    count = 0
    while count < 6: 
        print(count)
        count += 1
    
    print('end.....')

      3、如何结束while循环

      (1)把条件改为False
      (2)break:直接终止本层循环
    #把条件改为False
    
    db_name = "egon"
    db_pwd = "123"
    tag = True
    while tag:
        inp_name = input("请输入您的用户名: ")
        inp_pwd = input("请输入您的密码: ")
    
        if inp_name == db_name and inp_pwd == db_pwd:
            print("用户登录成功")
            tag = False
        else:
            print("用户账号或密码错误")
    
        print('其他。。。。。。。。。')
    
    #(2)break:直接终止本层循环
    db_name = "egon"
    db_pwd = "123"
    
    while True:
        inp_name = input("请输入您的用户名: ")
        inp_pwd = input("请输入您的密码: ")
    
        if inp_name == db_name and inp_pwd == db_pwd:
            print("用户登录成功")
            break
        else:
            print("用户账号或密码错误")
    
        print('其他。。。。。。。。。')
    4、死循环
    while True:
        print('Hello')
        input(">>: ")
    5、while+continue: 终止本次循环
      强调1:不要在continue之后编写同级别的代码
      强调2: 如果不想执行本次循环之后的代码,可以用continue,但是如果本次循环本来就没有要继续运行的后续代码了,就没必要加continue了
    #强调1:不要在continue之后编写同级别的代码
    #打印出1,2,5
    count = 0
    while count < 6: 
        if count == 3
        or count == 4:
            count += 1
            continue
            # count+=1 # 不要写在这里
        print(count)
        count += 1
    
    #强调2: 如果不想执行本次循环之后的代码,可以用continue,但是如果本次循环本来就没有要继续运行的后续代码了,就没必要加continue了
    db_name = "egon"
    db_pwd = "123"
    while True:
        inp_name = input("请输入您的用户名: ")
        inp_pwd = input("请输入您的密码: ")
        if inp_name == db_name and inp_pwd == db_pwd:
            print("用户登录成功")
            break
        else:
            print("用户账号或密码错误")
            # continue没必要写了
    6、while+else
    ps:while循环没被break打断的都叫正常死
    #打印出1,2,3
    count = 1
    while count < 6:
        print(count)
        if count == 3:
            break
        count+=1
    else:
        print('会在while循环正常死亡之后运行')
    
    #打印出1,2,3, 4,5
    count = 1
    while count < 6:
        print(count)
        # if count == 3:
        #     break
        count+=1
    else:
        print('会在while循环正常死亡之后运行')
    7、while循环嵌套
    while True:
        while True:
            while True:
                break
            break
        break     
        
    
    
    tag = True
    while tag:
        while tag:
            while tag:
                tag = False
        
    #用户登录成功后输入指令,输入0时,退出系统
    db_name = "egon"
    db_pwd = "123"
    
    while True:
        inp_name = input("请输入您的用户名: ")
        inp_pwd = input("请输入您的密码: ")
    
        if inp_name == db_name and inp_pwd == db_pwd:
            print("用户登录成功")
    
            while True:
                print("""
                0 退出
                1 取款
                2 提现
                3 转账
                """)
                cmd=input("请输入您的命令编号:")
                if cmd == "0":
                    break
                elif cmd == "1":
                    print("正在取款")
                elif cmd == "2":
                    print("正在提现")
                elif cmd == "3":
                    print("正在转账")
                else:
                    print("不知道的指令,请重新输入")
            break
        else:
            print("用户账号或密码错误")
    # tag的方式结束循环
    db_name = "egon"
    db_pwd = "123"
    
    tag = True
    while tag:
        inp_name = input("请输入您的用户名: ")
        inp_pwd = input("请输入您的密码: ")
    
        if inp_name == db_name and inp_pwd == db_pwd:
            print("用户登录成功")
    
            while tag:
                print("""
                0 退出
                1 取款
                2 提现
                3 转账
                """)
                cmd=input("请输入您的命令编号:")
                if cmd == "0":
                    tag = False
                elif cmd == "1":
                    print("正在取款")
                elif cmd == "2":
                    print("正在提现")
                elif cmd == "3":
                    print("正在转账")
                else:
                    print("不知道的指令,请重新输入")
    
    
        else:
            print("用户账号或密码错误")

    二、for循环

    1、概念:
    (1)for循环主要用于循环取值,例如列表、字典、字符串
    (2)for循环循环的次数取决于值的个数
    while循环循环的次数取决条件什么时候变为False或者什么时候执行break
    2、基本使用
    l = [1111, 222, 333, 444, 555]
    #用while循环
    i = 0
    while i < len(l):
        print(l[i])
        i += 1
    #用for循环
    #列表
    for x in l:
        print(x)
    
    #字典
    # d = {"k1": 111, "k2": 2222, "k3": 33333}
    # for k in d:
    #     print(k,d[k])
    
    #字符串
    # msg="hello world"
    # for x in msg:
    #     print(x)
    
    #列表中套列表
    l = [["aaa", 1111], ["bbb", 2222], ["ccc", 3333]]
    for x, y in l:  # x,y=["aaa",1111]
        print(x, y)
    3、for + break
    for x in [111,222,333,4444,555]:
        if x == 333:
            break
        print(x)
    4、for + continue
    for x in [111,222,333,4444,555]:
        if x == 333:
            continue
        print(x)
    5、for + else
    for x in [111,222,333,4444,555]:
        if x == 333:
            break
        print(x)
    else:
        print('=====>')

    6、for+range()
    #python2中得到的是列表,python3中得到的是一个个字符串
    python2
    # >>> range(1,5,2) # 起始位置,结束位置,步长
    # [1, 3]
    # >>>
    # >>> range(1,5) # 省略步长,默认为1
    # [1, 2, 3, 4]
    # >>>
    # >>> range(5) # 省略起始位置与步长,默认起始位置为0,步长为1
    # [0, 1, 2, 3, 4]
    #python3
    for x in range(0,5,1):  
        print(x)    # 0 1 2 3 4




  • 相关阅读:
    Blank page instead of the SharePoint Central Administration site
    BizTalk 2010 BAM Configure
    Use ODBA with Visio 2007
    Handling SOAP Exceptions in BizTalk Orchestrations
    BizTalk与WebMethods之间的EDI交换
    Append messages in BizTalk
    FTP protocol commands
    Using Dynamic Maps in BizTalk(From CodeProject)
    Synchronous To Asynchronous Flows Without An Orchestration的简单实现
    WSE3 and "Action for ultimate recipient is required but not present in the message."
  • 原文地址:https://www.cnblogs.com/guojieying/p/13274124.html
Copyright © 2020-2023  润新知