【问题标题】:Identical Strings won't match?相同的字符串不匹配?
【发布时间】: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


【解决方案1】:

看起来您已经找到了答案。 但我想说你最好使用'b'而不是'a'进行比较。 因为出于某种原因,您应该删除“:”。 :)

【讨论】:

  • …………该死的,它是 repl.it……Chromebook 和 Beta!导致可读性差。谢谢大声笑
猜你喜欢
  • 2020-07-07
  • 2018-08-18
  • 2013-10-24
  • 2017-12-05
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多