比较相等的字符串是否返回False? [英] Comparing equal strings returns False?

查看:53
本文介绍了比较相等的字符串是否返回False?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用基本的临时文件进行基本的临时登录验证,以假装,而返回True的不是。用户名检查有效,但密码检查无效

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

这是输出结果。密码与文本文件完全匹配,但返回false。用户名返回true。到底是什么问题?

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


推荐答案

必须使用.strip()函数删除行末的不可预见字符。

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

这篇关于比较相等的字符串是否返回False?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆