Python两个变量是相同的,但Python并不认为它们是相同的
我正在制作一个有登录名的程序;它根据用户的输入检查输入。但是,即使密码正确,它也不会接受他们输入了正确的密码:Python两个变量是相同的,但Python并不认为它们是相同的,python,Python,我正在制作一个有登录名的程序;它根据用户的输入检查输入。但是,即使密码正确,它也不会接受他们输入了正确的密码: 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 n
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")
当您运行
passwordText=linecache.getline(“logins.txt”,passwordLine)
时,它引用的文本文件中只有Har16和Google,在单独的行中,Har16位于顶部,它可能返回结尾带有\n
的字符串。您确定密码文本中没有空格或其他字符吗?欢迎使用StackOverflow。请阅读并遵循帮助文档中的发布指南。适用于这里。在您发布MCVE代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中,并重现您描述的问题。请尝试打印repr(passwordtrunt)
和repr(passwordText
。它们不一样-计算机不会说谎。请明确说明问题。除其他外,您需要显示正在比较的字符串,并以某种方式区分空白:前导/尾随空格、换行符等。