• python中for循环(有限循环)和while循环(无限循环)


    1. for循环

    # for是有限循环,while是无限循环
    
    # for后面也是可以接else
    _user = "zgzeng"
    _psw = "zgz"
    # 限制登陆3次,如果3次登陆失败了,就会自动退出
    for i in range(3):
        username = input("Your name:")
        password = input("Your password:")
        if username == _user and password == _psw:
            print("welcome to login our system!!!")
        else:
            print("Invalid your username or password!!!")
    
    
    
    # 当登陆3次都失败了,优化给出提示再退出
    # 方法1:使用标记
    flag = False
    for i in range(3):
        username = input("Your name:")
        password = input("Your password:")
        if username == _user and password == _psw:
            print("welcome to login our system!!!")
            # 当flag =True的时候,会执行后面if的代码
            flag = True
            break
        else:
            print("Invalid your username or password!!!")
    if  not flag:
        print("试了三次!")
    
    
    # 方法2:除了if后面可以接else外,for后面也可以接
    for i in range(3):
        username = input("Your name:")
        password = input("Your password:")
        if username == _user and password == _psw:
            print("welcome to login our system!!!")
            break
        else:
            print("Invalid your username or password!!!")
    else:
        print("已经试了三次!")
    # for循环后面可以接else,但不可以elif
    # for循环程序执行,当程序正常执行完成,就会执行else,如果不正常执行,就不会执行else,这里3次以内登陆之后,就会break跳出程序,这里就是不正常执行,就不会执行else程序,而3次以内没能登陆成功,就会执行到else程序

    方法1和方法2的效果都是一样,但是明显方法2更加简洁

    2.while循环

    while循环如果不加条件,就是一个死循环

    while也是可以接else,与for效果是一样的

    counter = 0
    while counter > 3:
        username = input("Your name:")
        password = input("Your password:")
        if username == _user and password == _psw:
            print("welcome to login our system!!!")
            # 当flag =True的时候,会执行后面if的代码
            flag = True
            break
        else:
            print("Invalid your username or password!!!")
        counter += 1
    else:
        print("已经试了三次!!!")
    # 程序优化每试3次,3次失败后,给出选择提示,是否继续尝试登陆
    # 思路:每当循环试了3次之后,程序就会给出选择,那么我们就需要每尝试3次之后,就将counter清零
    
    _user2 = "zgzeng"
    _pwd = "zgzeng" 
    counter = 0
    while counter < 3:
        username = input("请输入用户名:")
        password = input("请输入密码:")
        if _user2 == username and password == _pwd: 
            print("欢迎登陆我的程序")
            break
        else:
            print("您的用户名或者密码有误")
        counter += 1
        keep_going_choice = input("输入[y]将继续,其他退出")
        if keep_going_choice == "y":
            counter = 0
    # 这里的执行效果是每执行1次,程序就会问是否继续,并没有实现我们理想中的效果(3次询问一次)

    需要再加一个判断

    _user2 = "zgzeng"
    _pwd = "zgzeng"
    counter = 0
    while counter < 3:
        username = input("请输入用户名:")
        password = input("请输入密码:")
        if _user2 == username and password == _pwd:
            print("欢迎登陆我的程序")
            break
        else:
            print("您的用户名或者密码有误")
        counter += 1
        if counter == 3:  # 加一个判断
            keep_going_choice = input("输入[y]将继续,其他退出")
            if keep_going_choice == "y":
                counter = 0

    将循环和嵌套循环中的两次break进行关联也需要用到标记位

    flag = False
    # 目标:嵌套层循环break,我外层循环因为内层循环跳出也跳出,否则不跳出
    # 知识点:标记位
    for i in range(10):
        if i > 5:
            print(i)
        for j in range(10):
            print("第二层:",j)
            if j == 6:
                flag = True
                break
        if flag == True:   # 这就将两个break关联起来了
            break
  • 相关阅读:
    Java 流(Stream)、文件(File)和IO
    Java集合笔记
    希尔排序
    多线程
    查找算法
    快速排序
    leetcode题解58
    9.回文数
    SonarQube代码质量管理平台安装及使用
    用GitBlit 和 VS GitSourceControlProvider 搭建基于 Http(s) 的 Git 工作平台
  • 原文地址:https://www.cnblogs.com/zgzeng/p/12748246.html
Copyright © 2020-2023  润新知