出现这种现象分为两种情况:

  • 第1种可能情况:list[index]index超出范围
  • 第2种可能情况:list是一个空的 没有一个元素 进行list[0]就会出现该错误

python out of range python out of range 判定_while循环


以图片中的代码为例:在多线程当中,多个线程同时工作,只剩下一张票的时候,第一个进入shou_tick()函数的线程进行了while循环判断准备取走最后一张票时,后来的线程也在进行判断,当票被取走了后 ,后来的线程在取票时就会产生#list index out of range这个报错

python out of range python out of range 判定_人工智能_02


这个时候们可以选择,对list_tick进行非空判断,并且在锁中假如一个if判断如果list_tick为0就break,不再进行下边的赋值和删除

python out of range python out of range 判定_while循环_03