import nxppy
import time
class student: # Object giving first and last name, idCard and statut
def __init__(self, name, idCard, present):
self.name = name
self.idCard = idCard
self.present = None
class_TS4 = []
class_TS4.append(student('moureton etienne', '4DC3A150', None))
class_TS4.append(student('metzinger axel', '5FG998H2', None))
# print(class_1S4)
# Instantiate reader
mifare = nxppy.Mifare()
while True:
try:
uid = mifare.select()
for student in class_TS4:
# comparing uid to the each of the student's idCard attribute
if uid == student.idCard:
student.present = True
print(student.present)
print(student.name)
break # since uids are unique there is no point to check other students
# Permet le polling
except nxppy.SelectError:
pass
time.sleep(1)
您好,世界!我需要您的帮助,以最快的速度……我在Hihg School的一个项目中工作,但被阻止了。我从 Python 开始,然后在运行Raspbian的Pi3 B +上进行编程。
我必须阅读某些NFC卡的 UID ,并且该卡工作正常。但是我必须检查我读取的UID是否与我的课程之一的“ self.idCard” 相匹配。如果是,我想将包含UID的对象的“ self.present” 更改为True。
至少,我的目标是像这样增加30名“学生”,并且程序可以告诉我哪个人通过了他的卡。
idCard的UID对于每个学生而言都是唯一且恒定的。
感谢所有<3
答案 0 :(得分:2)
您当前将读取的uid
与类的实例进行比较,该实例将始终为False
。您应该将苹果与苹果进行比较:
while True:
uid = mifare.select()
for student in class_TS4:
# comparing uid to each of the student's idCard attribute
if uid == student.idCard:
student.present = True
break # since uids are unique there is no point to check other students
另一种更有效的方法是使用字典。这样,uid查找将为O(1)
:
uids_to_student = {student.idCard: student for student in class_TS4}
while True:
# Read UID data
uid = mifare.select()
try:
uids_to_student[uid].present = True
except KeyError:
print('No student with this UID exist in class')
顺便说一句,student.__init__
接受一个present
参数,但不做任何事情。从签名中删除它,使用它或给它一个默认值:
class student:
def __init__(self, name, idCard, present=None):
self.name = name
self.idCard = idCard
self.present = present