首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复python中的'list index out of range‘错误?

如何修复python中的'list index out of range‘错误?
EN

Stack Overflow用户
提问于 2019-05-28 16:53:16
回答 1查看 398关注 0票数 1

我正在设置考勤系统的RFID阅读器。但是当我把我的标签放到阅读器上时,它被标签id中断了-我把它外部存储到数据库中。

代码语言:javascript
复制
#//////place your tag//////

print("Now place your tag to write")
rdr.wait_for_tag()

(error, data) = rdr.request()
if not error:
    print("\nDetected: " + format(data, "02x"))

(error, uid) = rdr.anticoll()
if not error:
    print("Card read UID: " +str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]))

tagid = str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]) 

print("Written..!")
print(tagid)


cursor.execute("insert into rfid_check (uid,firstname,age,tag_id) values ('%s','%s','%s','%s')" %(user_id,fname, ag_e, tagid))
connection.commit()
print("Data was successfully Added...!")

tagid = str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]) 
IndexError: list index out of range
EN

回答 1

Stack Overflow用户

发布于 2019-05-28 17:20:44

代码结构不正确。尝试用这个来缩进rdr.anticoll(),只要标记请求不正确,就不会发生任何处理。

尝试下面这行代码,以获得更好的可视化效果:

代码语言:javascript
复制
print("Now place your tag to write")
rdr.wait_for_tag()

(error, data) = rdr.request()
if not error:
    print("\nDetected: " + format(data, "02x"))

    (error, uid) = rdr.anticoll()
    if not error:
        print("Card read UID: " +str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]))

        tagid = str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]) 

        print("Written..!")
        print(tagid)
        cursor.execute("insert into rfid_check (uid,firstname,age,tag_id) values ('%s','%s','%s','%s')" %(user_id,fname, ag_e, tagid))
        connection.commit()
        print("Data was successfully Added...!")
else:
    print("Unsuccessful")
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56338412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档