Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

python - Comparing equal strings returns False?

So I'm making a basic makeshift login validate thing with just a basic text file to "pretend" and what should return True is not. The username check works but not the password check

@staticmethod
def validate_login(filename,username,password):
    file = open(filename,'r')
    file.readline()
    for line in file:
        i = [i for i, char in enumerate(line) if char == '|']
        print username == line[i[0] + 2:i[1]-1]
        print password == line[i[1] + 2:]
        print password
        print line[i[1] + 2:]
        if username == line[i[0] + 2:i[1]-1] and password == line[i[1] + 2:]:
            file.close()
            print "Login Successful."
            return True

    file.close()
    print "Failed to login. Invalid username or password."
    return False

This is the output result. The password matches exactly with the text file yet returns false. The username returns true.What exactly is the issue?? It is driving me insane.

True <--- username matched correctly returns true
False <---passwords apparently don't match, returns false.
DopeFiend97 <--password
DopeFiend97
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Had to use .strip() function to remove unforeseen characters at the end of the line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...