【发布时间】:2019-12-28 06:31:59
【问题描述】:
我通过将用户名和密码存储在单独的文件中并程序访问它,然后读取密码并将其与用户输入的内容进行比较,从而在 python 中创建了一个帐户登录系统。它可以读取文件中的密码,但由于某种原因,当它与用户输入的密码进行比较时,它总是说它是错误的。
我尝试将实际密码与用户的输入进行比较,我知道它正在正确读取文件,因为我让它打印出它读取的内容并打印了正确的密码。我还让它打印了用户的输入,以确保它是正确的并且也可以正常工作。
您知道,该文件已经存在,其中包含第二行的密码,并且它会找到正确的文件,因为该文件是以它所针对的帐户命名的。
Account = str(input("Enter the username. "))
Account_Password = str(input("Enter the password. "))
AccountFileName = (Account + ".txt")
with open(AccountFileName,"r") as AF:
for x, line in enumerate(AF):
if x == 1:
Account_Password_Check = (line)
if Account_Password == Account_Password_Check:
print("Welcome, " + Account + "!")
else:
print("Either the username or password were incorrect.")
如果用户输入的和密码一样,它应该打印,“Welcome (username here)!”如果它们不同,则应打印“用户名或密码不正确。”
如果你知道出了什么问题,请告诉我。
谢谢。
【问题讨论】:
-
该行可能包含不可见的空格。在你的循环中尝试
Account_Password_Check = line.strip()。 -
当你从文件中读取时,你在行尾有
'\n',你必须strip()它 -
@furas 谢谢你,这对我帮助很大!
标签: python string file variables