IndexError:使用自定义功能时列表分配索引超出范围

时间:2019-12-13 19:13:23

标签: python python-3.x

执行以下代码时,我得到IndexError: list assignment index out of range

def tcheb (a,b,n):
    V=[]
    for j in range(1,n) :
        for k in range (1,n):
                V[k]=((a+b)/2) + (((a-b)/2) * (np.cos((k-1)/(n-1)*np.pi)))
        return V.append(k)
tcheb(-5,5,2)

1 个答案:

答案 0 :(得分:0)

a = [0] * 10是初始化大小清单的方式

def tcheb (a,b,n):
    V=[0] * 10
    for j in range(1,n) :
        for k in range (1,n):
                V[k]=((a+b)/2) + (((a-b)/2) * (np.cos((k-1)/(n-1)*np.pi)))
        return V.append(k)
tcheb(-5,5,2)