python - 试图做一个简单的比较,在涉及RFID的python中,没有比较成功
问题描述
我有一个 RFID 标签连接到我的树莓派,我在 RFID 标签上写了一个测试字符串,字母“ab”。
def read():
reader = SimpleMFRC522()
#stores id number and a 'password' (ab is stored as text)
try:
id,text = reader.read()
finally:
GPIO.cleanup()
return text
if __name__ == '__main__':
password = read()
print(password)
if password == 'ab':
print("password: "+password)
else:
print("Incorrect tag.")
当我跑步时,我得到:
ab
Incorrect tag.
我应该得到
password: ab
在我的程序中,我有一个 read() 函数,它只返回标签上的文本“ab”。
在 main 中,我将分配给 read() 的变量与字符串“ab”进行比较,它应该只打印
password: ab
相反,它被视为不正确,并转到 else 语句。
有谁知道这可能是什么?
我做了什么:
我尝试通过将 str() 包裹在它们周围来强制将密码和“ab”作为字符串,即使它们已经是字符串。
我已经检查了密码的类型。它是一个字符串。
print(type(password))
我还将声明更改为
if True:
只是看看打印语句是否会执行。它做了。
我不确定这个错误是什么考虑到我读到标签的内容中没有空格,它实际上只是字母“ab”
解决方案
你必须使用strip()
函数。
if(password.strip() == "ab"):
它必须工作。
推荐阅读
- c++ - 使用异步是否总是让 C++ 代码更快
- javascript - 在本机反应中将javascript数组类型传递给java函数
- python - Python文件数据位置
- python - 二十一点游戏中导致循环错误和无限滚动
- c# - .NET Core 3.1 - RSA.ImportRSAPrivateKey 引发 CryptographicException
- ruby-on-rails - 如何通过角色和电影设计 has_many:在 Rails 6 中
- apache-kafka - 我可以通过现有的 CLI 获得一个简单的独立 Kafka Producer,而无需拖入整个 Kafka 代理库吗
- flutter - 在颤振应用程序中添加 Alan_AI 依赖项会导致错误
- ios - Xamarin iOS 和 iOS 14 UISplitViewController 三列
- reactjs - 增量静态再生中的重新验证过程如何工作?