Python &引用;ValueError:list。删除(x):x不在列表中;

Python &引用;ValueError:list。删除(x):x不在列表中;,python,python-3.x,list,valueerror,Python,Python 3.x,List,Valueerror,我不知道怎么了,我已经浏览了很多帖子,但还没有得到答案。我的代码基本上是一个更大程序的一部分,这个程序太大了,无法上传。这是一个tkinter应用程序,当我点击按钮a时,“def pressa”工作。它工作得很好,但每次在某个时候我都会出现上面的错误,但是如果我再次单击按钮,它就会工作,直到再次出现错误。这就是错误: "Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\ReLoaDe

我不知道怎么了,我已经浏览了很多帖子,但还没有得到答案。我的代码基本上是一个更大程序的一部分,这个程序太大了,无法上传。这是一个tkinter应用程序,当我点击按钮a时,“def pressa”工作。它工作得很好,但每次在某个时候我都会出现上面的错误,但是如果我再次单击按钮,它就会工作,直到再次出现错误。这就是错误:

"Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ReLoaDeR\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\ReLoaDeR\AppData\Local\Programs\Python\Python37\project\project.py", line 31, in pressa
    y=(ap[epilogh])
IndexError: list index out of range"


为什么有时我的代码工作正常,然后意外地出现此错误?

您的问题是
ap
小于
er

if er!=[] and ap!=[]:
epilogh=random.randint(0, len(er)-1) //Get a number larger than size of ap. 
x=(er[epilogh])
y=(ap[epilogh]) // ap doesnt have that index. 
if x in er:
    er.remove(x)
if y in ap:
    ap.remove(y)
q.set(x)
an.set(y)

你应该得到另一个随机数来从
ap

获取数据,添加一些测试用例来显示它何时工作,何时不工作。基本上,这是一个更大程序的一部分,它太大了,无法上传。这是一个tkinter应用程序,当我点击按钮a时,“def pressa”工作。它可以正常工作,但每次我都会遇到上面的错误,但是如果我再次单击按钮,它就会工作,直到再次出现错误。索引超出范围错误应该会给你一个提示。如果有时会发生这种情况,那么它肯定与传递给它的数据有关。我会尝试在每次调用函数之前打印数据,并尝试对其进行调试,查看导致异常的数据和工作的数据。但我想从ap列表中选择与er的位置相同的项。我该怎么做?你需要检查长度是否相同。如果ap的元素较少怎么办?
if er!=[] and ap!=[]:
epilogh=random.randint(0, len(er)-1) //Get a number larger than size of ap. 
x=(er[epilogh])
y=(ap[epilogh]) // ap doesnt have that index. 
if x in er:
    er.remove(x)
if y in ap:
    ap.remove(y)
q.set(x)
an.set(y)