Как исправить ошибку 'list index out of range' в python? - PullRequest
0 голосов
/ 28 мая 2019

Я устанавливаю RFID-считыватель для системы посещаемости.Но когда я помещаю свой тег в считыватель, он был прерван с идентификатором тега, который я внешне сохранил в базе данных.

#//////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

1 Ответ

1 голос
/ 28 мая 2019

Ваша структура кода неверна.Попробуйте сделать отступ в rdr.anticoll (), если запрос тега неверен, процесс не будет выполняться.

Попробуйте строку кодов для лучшей визуализации:

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")
...