首页 > 解决方案 > 试图做一个简单的比较,在涉及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 语句。

有谁知道这可能是什么?

我做了什么:

我还将声明更改为

if True:

只是看看打印语句是否会执行。它做了。

我不确定这个错误是什么考虑到我读到标签的内容中没有空格,它实际上只是字母“ab”

标签: pythonraspberry-pirfid

解决方案


你必须使用strip()函数。

if(password.strip() == "ab"): 它必须工作。


推荐阅读