不明白为什么我不断收到有关不受支持的操作数类型的TypeError

时间:2018-11-04 18:50:54

标签: python

我不断收到此错误:

File "C:/Users/EEWOR/OneDrive/Documents/httgh.py", line 15, in s
    total+=((powerfirst))/(factfirst)*(x**(firstxpower))
TypeError: unsupported operand type(s) for /: 'function' and 'function'

运行此代码时:

def s(x) :
    def factfirst(n):
        initalvalue=1
        for y in range(1,2*x):
            initalvalue=initalvalue*y
        return (factfirst)
    def powerfirst(n) :
        (-1)**(n-1)
        return (powerfirst)
    def firstxpower(n) :
        (2*n)-1
        return (firstxpower)
    for n in range(1,x+1):
        total=0
        total+=((powerfirst))/(factfirst)*(x**(firstxpower))
    return sum(total)
print(s(5))

1 个答案:

答案 0 :(得分:1)

def s(x) :
    def factfirst(n):
        initalvalue=1
        for y in range(1,2*x):
            initalvalue=initalvalue*y
        return (factfirst) // you are returning function not a value or result
    def powerfirst(n) :
        (-1)**(n-1)
        return (powerfirst) //you are returning function not a value or result
    def firstxpower(n) :
        (2*n)-1
        return (firstxpower) //you are returning function not a value or result
    for n in range(1,x+1):
        total=0
        total+=((powerfirst))/(factfirst)*(x**(firstxpower))// you are dividing functions because your returns return functions
    return sum(total) //cannot sum a single value
print(s(5))

此嵌套函数的用法不好,很难阅读。 试试这个:

def factfirst(n):
    initalvalue=1

    for y in range(1,2*n):
        initalvalue=initalvalue*y
    return initalvalue

def powerfirst(n) :
    return (-1)**(n-1)

def firstxpower(n) :
    return (2*n)-1

def s(x) :
    for n in range(1,x+1):
        total=0
        total+=(powerfirst(n)//factfirst(n)*(x**firstxpower(n)))
    return total
print(s(5))

这正在起作用,您可以进行进一步的更改。