zoukankan      html  css  js  c++  java
  • python学习:利用循环语句完善输入设置

    利用循环语句完善输入设置

    使用for循环:

    代码1:
    _user = "alex"

    _password = "abc123"

    for i in range(3):
    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _password:
    print("Welcome %s login ..." % _user)
    break
    else:
    print("Invalid username or password !")
    代码2:
    _user = "alex"
    _password = "abc123"

    passed_authentication = False

    for i in range(3):
    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _password:
    print("Welcome %s login ..." % _user)
    passed_authentication = True #真,成立
    break
    else:
    print("Invalid username or password !")

    if not passed_authentication:
    print("您的输入次数已经达到上限!") #只有在True的情况下,条件成立
     代码3:
    _user = "alex"
    _password = "abc123"

    #passed_authentication = False

    for i in range(3):
    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _password:
    print("Welcome %s login ..." % _user)
    #passed_authentication = True #真,成立
    break
    else:
    print("Invalid username or password !")
    else:
    print("您的输入次数已经达到上限!")#for循环正常结束,就可以执行下面的else语句

    使用while循环:

    代码:
    _user = "alex"
    _password = "abc123"

    counter = 0
    while counter < 3:
    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _password:
    print("Welcome %s login ..." % _user)
    break
    else:
    print("Invalid username or password !")
    counter += 1
    else:
    print("您的输入次数已经达到上限!")
     代码2
    _user = "alex"
    _password = "abc123"

    counter = 0
    while counter < 3:
    username = input("Username:")
    password = input("Password:")
    if username == _user and password == _password:
    print("Welcome %s login ..." % _user)
    break
    else:
    print("Invalid username or password !")
    counter += 1
    if counter == 3:
    keep_going_choice = input("还想玩么?[y/n]")
    if keep_going_choice == "y":
    counter = 0
    else:
    print("您的输入次数已经达到上限!")
     
  • 相关阅读:
    js或css文件后面的参数是什么意思?
    查看mysql语句运行时间的2种方法
    lumia手机wp系统应用列表如何设置按照拼音
    每一个你不满意的现在,都有一个你没有努力的曾经。
    一行代码实现防盗链!
    请写一个php函数,可以接受任意数量的参数
    ajax处理缓冲问题
    Textarea自动适用高度且无滚动条解决方案
    PHP Warning exec() has been disabled for security reasons怎么办
    各种字符编码方式详解及由来(ANSI,UNICODE,UTF-8,GB2312,GBK)
  • 原文地址:https://www.cnblogs.com/pl-2018/p/9419186.html
Copyright © 2011-2022 走看看