Python 类型错误:';非类型';对象是不可编辑的。为什么会出现这个错误?

Python 类型错误:';非类型';对象是不可编辑的。为什么会出现这个错误?,python,recursion,Python,Recursion,函数compute_root使用牛顿逐次逼近法找到多项式零点的足够好的逼近(问题就在这里)。函数evalPoly计算特定x值处多项式的值,函数ddx2计算多项式的导数 poly2 = (2,3,1,5) #poly2 represents the polynomial 5x^3+x^2+3x+1 def evalPoly(poly,x): degree = 0 ans = 0 for index in poly: ans += (index * (x**

函数compute_root使用牛顿逐次逼近法找到多项式零点的足够好的逼近(问题就在这里)。函数evalPoly计算特定x值处多项式的值,函数ddx2计算多项式的导数

poly2 = (2,3,1,5) #poly2 represents the polynomial 5x^3+x^2+3x+1

def evalPoly(poly,x):
    degree = 0
    ans = 0
    for index in poly:
        ans += (index * (x**degree))
        degree += 1
    return ans  

def ddx2(tpl):
    lst = list(tpl)
    for i in range(len(lst)):
        lst[i] = lst[i]*i
        if i != 0:
            lst[i-1] = lst[i]
    del lst[-1]
    tpl = tuple(lst)

def compute_root(poly,x_0):
    epsilon = .001
    numGuesses = 1
    if abs(evalPoly(poly,x_0)) <= epsilon:
        ans = (evalPoly(poly,x_0),numGuesses)
        print ans
        return ans
    else:
        x_1 = x_0 - (evalPoly(poly,x_0)/evalPoly(ddx2(poly),x_0))
        # This is Newton's method of getting progressively better / 
        # "guesses"
        compute_root(poly,x_1)
        x_0 = x_1
        numGuesses += 1
        return x_0
        return poly

compute_root(poly2,2) #Here I call the function *compute_root*
poly2=(2,3,1,5)#poly2表示多项式5x^3+x^2+3x+1
def evalPoly(聚乙烯,x):
度=0
ans=0
对于多边形中的索引:
ans+=(指数*(x**度))
学位+=1
返回ans
def ddx2(tpl):
lst=列表(tpl)
对于范围内的i(len(lst)):
lst[i]=lst[i]*i
如果我0:
lst[i-1]=lst[i]
德尔一世[-1]
tpl=元组(lst)
def compute_root(多边形,x_0):
ε=0.001
numguesss=1
如果abs(evalPoly(poly,x_0))您的函数
ddx2(tpl)
缺少返回值。

函数evalPoly 一旦返回ans,它就不会返回degree:您可能需要使用return(ans,degree)

类似地,在else循环中

此外,ddx2不返回任何函数。

更改:

def ddx2(tpl):
    lst = list(tpl)
    for i in range(len(lst)):
        lst[i] = lst[i]*i
        if i != 0:
            lst[i-1] = lst[i]
    del lst[-1]
    tpl = tuple(lst)
致:

它缺少一个
返回

不过,你还有一个问题,我不清楚你的意图是什么。此函数返回两次,实际上只返回第一次(
ans
):

下一个问题是达到递归极限。我建议您使用循环而不是递归来解决这个问题

poly5 = (-13.39, 0.0, 17.5, 3.0, 1.0) # represents the polynomial:
       # x^4+3x^3+17.5x^2-13.39 


def evalPoly(poly,x):
    degree = 0
    ans = 0
    for index in poly:
        ans += (index * (x**degree))
        degree += 1 
    return float(ans)
    return degree   

def ddx2(tpl):
    lst = list(tpl)
    for i in range(len(lst)):
        lst[i] = lst[i]*i
        if i != 0:
            lst[i-1] = lst[i]
    del lst[-1]
    tpl = tuple(lst)
    return tpl

def compute_root(poly,x_0,numGuesses):
    epsilon = .001
    ans = []
    if abs(evalPoly(poly,x_0)) <= epsilon:
        ans.append(x_0)
        ans.append(numGuesses) 
        ans = tuple(ans)
        print ans
        return ans
    else:
        numGuesses += 1
        x_0 = x_0 - (evalPoly(poly,x_0)/evalPoly(ddx2(poly),x_0))
        compute_root(poly,x_0,numGuesses)

    compute_root(poly5,.1,1)
def ddx2(tpl):
    lst = list(tpl)
    for i in range(len(lst)):
        lst[i] = lst[i]*i
        if i != 0:
            lst[i-1] = lst[i]
    del lst[-1]
    tpl = tuple(lst)
def ddx2(tpl):
    lst = list(tpl)
    for i in range(len(lst)):
        lst[i] = lst[i]*i
        if i != 0:
            lst[i-1] = lst[i]
    del lst[-1]
    tpl = tuple(lst)

    return tpl
def evalPoly(poly,x):
    degree = 0
    ans = 0
    for index in poly:
        ans += (index * (x**degree))
        degree += 1
    return ans
    return degree