这是我第一次使用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)
答案 0 :(得分:1)
这是我所了解问题的解决方案
除非用户输入正确的密码,否则行if typed_password == "12345":
将不起作用
修复:在elif语句中添加它
如果result
显示所需的消息,例如Access Granted
或Access 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!!')