【问题标题】:Why is my password programme code not working?为什么我的密码程序代码不起作用?
【发布时间】:2018-10-07 08:27:24
【问题描述】:

代码只会让我猜一次。有人可以告诉我我的代码有什么问题吗?

挑战:

编写一个程序,将密码设置为“Gain Access”并询问 用户输入密码并不断询问直到密码正确 输入,然后说“接受”。程序应该计算多少 用户已经尝试并在他们完成后告诉他们 接受。

enter code here
password = 'Gain access'
count = 0
input = input("Enter the password: \n")

while input != password:
    print("Incorrect password! Please try again: \n")
    count = count + 1
    print("You have now got your password wrong " + str(count) + " times. \n")

    if(count < 5):
        print("Access denied, please contact security to reset your password.")
        break
    else:
        print("Accepted, welcome back.")
        print("You had " + str(count) + " attempts until you got your password right.")

【问题讨论】:

  • 这是什么语言?
  • Python 是正在使用的语言。
  • 在这种情况下还包括控制台的输出。我假设当您最初输入错误密码时,它会非常快地连续输出 5 次“拒绝访问”?

标签: python passwords


【解决方案1】:

您应该始终包含您正在使用的编程语言,就像前面提到的 simonwo 一样。

在我看来像 Python。我想这条线input = input("Enter the password: \n") 也需要跟在while input != password: 之后。否则你只能输入一次密码,然后它直接执行所有5个循环。但是您不应该分配input,因为这是您要从中获取输入的函数。

执行user_input = input("Enter the password: \n") 之类的操作。所以你的代码应该是这样的:

...
user_input = input("Enter the password: \n")

while user_input != password:
    print("Incorrect password! Please try again: \n")
    user_input = input("Enter the password: \n")
    ... Your existing code here

但请注意,如果用户在第一次尝试时输入了正确的密码,则不会收到通知。您可以在第一次阅读用户输入后插入一个检查,如果它与所需的密码匹配,则打印您的欢迎短语。

【讨论】:

  • 谢谢你,抱歉这是我的第一篇文章。
  • 没问题,这就是为什么我们会提示您如何改进您的问题 :) 我的建议是否解决了您的问题?
  • 很高兴能帮上忙!如果您可以通过单击向上和向下投票按钮下方的勾号来接受我的回答,我将不胜感激:)
猜你喜欢
  • 2014-01-23
  • 2013-08-06
  • 2014-04-10
  • 2016-01-23
  • 2011-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多