我正在制作一个有登录的程序;它根据用户的输入检查输入。但是,它不会接受他们输入了正确的密码,即使它是正确的:
for i in range(5):
existingUsername = input("What is your user name?")
if existingUsername in open('logins.txt').read():
with open("logins.txt", "r") as myFile:
for num, line in enumerate(myFile, 1):
if existingUsername in line:
passwordLine = num + 1
passwordAttempt = input(str("What is your password?"))
passwordText = linecache.getline("logins.txt", passwordLine)
if passwordAttempt == passwordText:
print("That is correct")
quizSelection()
break
else:
print("That doesn't seem to match. Please try again")
它引用的文本文件中只有Har16和Google,顶部是Har16的单独行
答案 0 :(得分:0)
当您运行passwordText = linecache.getline("logins.txt", passwordLine)
时,如果您的文件有多行,则可能会在结尾处返回带有\n
的字符串。