检查密码脚本不起作用-请检查我的代码是否有错误

时间:2019-01-19 08:09:06

标签: python-3.x

这是我第一次使用StackOverflow键入问题,任何人都可以检查我的代码并告诉我什么地方似乎不对吗,我想检查密码的输入是否正确,

当我在终端中运行该脚本时,它会显示“输入您的密码”行,但是当我输入密码时,无论密码正确与否,它都不会执行任何操作,并且该脚本会在不显示任何消息的情况下结束我在if语句中输入的两条消息,希望我能很好地描述问题...

password_file = open('SecretPasswordFile.txt')
secret_password = password_file.read()
result = ""
typed_password = input("Enter your Password!: ")

if typed_password == secret_password:
    result += "Access granted!"
    if typed_password == "12345":
        result += "That password is one that an idiot puts on their luggage!!!"
else:
    print('Access Denied!!')
print(result)

1 个答案:

答案 0 :(得分:1)

这是我所了解问题的解决方案

  1. 除非用户输入正确的密码,否则行if typed_password == "12345":将不起作用

    修复:在elif语句中添加它

  2. 如果result显示所需的消息,例如Access GrantedAccess Denied!!That password is one that an idiot puts on their luggage!!!

    修复:使用print()功能

固定代码

password_file = open('SecretPasswordFile.txt')
secret_password = password_file.read().rstrip() # fix : added rstrip() to trim whitespaces from the right side
typed_password = input("Enter your Password!: ")

if typed_password == secret_password:
    print ("Access granted!")
elif typed_password == "12345":
     print("That password is one that an idiot puts on their luggage!!!")
else:
    print('Access Denied!!')