【问题标题】:Display number of attempts left for incorrect password, accept if user inputs correct Password显示密码错误的尝试次数,如果用户输入正确的密码则接受
【发布时间】:2020-06-16 03:13:13
【问题描述】:

我一周前开始了我的编程之旅,之前的编码知识为零。 我还观看了有关如何发布堆栈溢出问题的视频,因此希望获得建设性反馈。

详情:

语言 - Python

版本 - 3.8.3

编辑器 - VS 代码

我正在尝试编写一个程序,该程序接受用户输入并检查它是否是有效密码。如果是,则显示“正确”并退出程序,如果不正确,则显示剩余尝试次数,总共3次,并再次要求输入。

我刚刚在我的教程中到达了 WHILE 循环部分,并用它来制作程序。

msg = input("What's the secret password: ")

# Password attempt is 3 times, starts at 2, then 1 and ends when counter is 0
num = 2  

# bananas is the correct password
while msg != "bananas":  
    if num == 0:
        print("Too many wrong attempts. You are locked out!")  # exhausted all 3 attempts, should print given message.

    else:
        print(f"Wrong Password! You have {num} chances left")  # password incorrect, display attempts left
        msg = input("What's the secret password: ")
        num = num - 1

print("Correct") 

问题

如果我立即运行它并提供正确的密码,或者在尝试 1-2 次失败后,它会按预期运行。 但是如果我输入错误 3 次,它就会进入显示消息的循环

错误尝试太多。你被锁定了!

并且永远打印出来。我无法弄清楚在哪里退出循环,以便它只显示一次消息并退出程序。

请帮助解决问题。

2 incorrect password, 1 correct in 3rd attempt

all 3 incorrect attempts

【问题讨论】:

    标签: python python-3.x loops while-loop


    【解决方案1】:

    试试这个结构:

    num = 3
    while input("What's the secret password: ") != "bananas" and num-1 != 0:
        num -= 1
        print(f"Wrong Password! You have {num} chance" + ['_', ' ', 's '][num] + "left")
    print("Correct") if num else print("Too many wrong attempts. You are locked out!")
    

    输入可直接用于 while 条件 - 如果您还剩零次尝试或获得正确的密码,while 循环将中断,if num:(num 大于零 - True)会显示相应的消息。

    作为一个函数:

    def pswd_entry(num=3):
        while input("What's the secret password: ") != "bananas" and num-1 != 0:
            num -= 1
            print(f"Wrong Password! You have {num} chance" + ['_', ' ', 's '][num] + 
        "left")
        print("Correct") if num else print("Too many wrong attempts. You are locked 
        out!")
    

    注意:pswd_entry(num=3) 有一个可选参数 num,如果留空则默认为 3


    解释

    我们的尝试次数:

    num = 3
    

    While 循环

    第一个条件: input("What's the secret password: ") != "bananas"

    input() 将等到用户输入一个值,如果该值不是“bananas”,则 while 循环不会中断。

    第二个条件: num-1 != 0

    这将检查尝试次数(减一)不 (!=) 等于 0。

    如果条件 1 and 满足条件 2,while 循环将运行。

    减去一次尝试num -=1(与num = num -1 相同),然后写入剩余机会数的警告。

    +['_', ' ', 's '][num] 将选择是否将's' 添加到机会/秒 - 例如,如果还剩 1 次机会(即 num = 1),则只需将 ' '(空格)添加到 'chance'

    如果num = 2:

    ['_', ' ', 's '][2] - ([0 element, 1 element, 2 element][select 2 element]) = 's' 添加到chance -> chances

    while input("What's the secret password: ") != "bananas" and num-1 != 0:
        num -= 1
        print(f"Wrong Password! You have {num} chance" + ['_', ' ', 's '][num] + "left")
    

    最后 - 打印声明

    print("Correct") if num else print("Too many wrong attempts. You are locked out!")
    

    如果num !=0 的值if num 将执行print("Correct") - if 检查这里检查num 有一个值并且不为零(假) - 否则它将打印锁定消息(来自else)。

    【讨论】:

    • 感谢豹子,我会通过这个并尝试理解逻辑。
    • @obelisk 不用担心 - 请随时提出任何具体问题 - 非常感谢
    • 请多多包涵。我很高兴有这么多人站出来提供帮助,有些人提供了多种方法。我需要时间来完成所有这些并了解它是如何工作的。
    • 嗨@leopardxpreload,我认为代码存在一些问题,(1)我认为它应该是“num -= 1”,这会减少每次迭代中num的值;而不是“num = -1”,它将新值 -1 分配给 num。 (2) 如果我在第 1 次或第 2 次尝试中输入正确的密码,它接受并显示“正确”,但如果我输入 2 次错误尝试并在第 3 次尝试中正确输入,它仍然给我“错误尝试太多。您被锁定了! "
    • @obelisk 你好! - 这次我为你做了更多的测试 - 希望新的编辑能更好地工作!
    【解决方案2】:

    这里的代码不要重复输入语句

    password, times = 'bananas', 3
    while True:
        times -= 1
        msg = input("What's the secret password: ")
        if msg == password:
            print("Correct")
            break
        elif times > 0:
            print(f"Wrong Password! You have {times} chances left")
        else:
            print("Too many wrong attempts. You are locked out!")
            break
    

    功能很好地将您的工作分成小部分。你可以找到这个和上面的区别。

    def login(password, times=3):
        while True:
            times -= 1
            msg = input("What's the secret password: ")
            if msg == password:
                print("Correct")
                return True
            elif times > 0:
                print(f"Wrong Password! You have {times} chances left")
            else:
                print("Too many wrong attempts. You are locked out!")
                return False
    
    result = login('bananas')
    if result:
        print('Login sucessful !')
    else:
        print('Login failed !')
    

    【讨论】:

    • 感谢杰森。我会尝试这两种方法。尝试了解它是如何工作的。当我了解了函数后,我会再尝试一遍。
    • 对于您的第一个代码:如果我输入 3 个错误密码,它会输出 - 密码错误!您还有 0 次机会 错误尝试次数过多。你被锁定了!我不想要线路 - 密码错误!您还有 0 次机会。所以我更新了最后一个 ELSE 语句。并想修改您的原始答案。希望没事。如果您认为这是一种改进,请告诉我。
    • 更新语句作为登录的一般步骤。
    • 嗨。我不明白你的评论。请您澄清一下,以便我有一个更好的主意。也感谢您更新您的代码。我正在运行它以了解逻辑
    • 只需三步: 1. 输入密码,“正确”信息退出。 2. 如果没有并且有更多的机会重新输入密码,则显示之前的输入错误以及还剩下多少机会。 3.如果没有机会,显示锁定并退出。
    【解决方案3】:

    通过将其放入函数中,您可以在打印后使用return 退出程序。

    def main():
        msg = input("What's the secret password: ")
    
        # Password attempt is 3 times, starts at 2, then 1 and ends when counter is 0
        num = 2  
    
        # bananas is the correct password
        while msg != "bananas":  
            if num == 0:
                print("Too many wrong attempts. You are locked out!")  # exhausted all 3 attempts, should print given message.
                return
            else:
                print(f"Wrong Password! You have {num} chances left")  # password incorrect, display attempts left
                msg = input("What's the secret password: ")
                num = num - 1
    
        print("Correct")
    
    
    main()
    

    【讨论】:

    • 谢谢。我认为它应该是 def main(): 而不是 def main() ...我的编辑器显示了一个错误,所以我猜这会解决它。没有函数也没有办法做到这一点,因为我还没有达到那么远。
    • @obelisk 是的,你是对的。你可以这样做,但它不会那么干净。
    • 你能用那个方法帮忙吗?感谢您对该功能的帮助。非常感谢
    • @obelisk 不,因为它很差,并且会导致很多问题。
    猜你喜欢
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多