这是我第一次使用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)
这是我已经理解的问题的解决方案
-
除非用户输入正确的密码,否则第
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!!')