0

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

挑战:

编写一个程序,将密码设置为“获得访问权”并要求用户输入密码并不断询问,直到输入正确的密码,然后说“接受”。程序应该计算用户进行了多少次尝试,并在他们被接受后告诉他们。

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.")
4

1 回答 1

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

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

于 2018-10-07T08:41:53.793 回答