【发布时间】:2017-02-20 12:30:30
【问题描述】:
我正在学习 Python,我正在尝试为一个更大的项目制作一个简单的登录程序。
这段代码:
def _login():#Login Function
username = input("Username:\n")
password = input("Please enter your password:\n")
with open("logins", "r") as log:
for line in log:
compound = str(username + password)
a = line
b = str(re.sub(':', '', a))
if a == compound:
print("Success")
_login() searches through a text file where **user:pass** are on individual lines displayed as such in the bold text.
然后,这将获取您输入的用户名和密码的字符串,将其连接起来并对文件中的用户名/密码执行相同的操作,排除通过正则表达式过滤的“:”。
问题:
用户输入的组合字符串(var compound)和文本文件中过滤后的字符串(var a) 在比较时不会返回 true,即使它们都等于用户 "UserPass"
我将如何解决这个问题?
干杯
【问题讨论】:
-
您是否尝试过打印每个字符串的
repr?我想你会发现,正如口译员告诉你的那样,它们并不相等。 -
啊废话,我做了 type()... :P
-
我明白了,谢谢,哈哈!文件中的密码包含“\n”的一个实例!
标签: python python-3.x