如何修复“ /:'int'和'function'的TypeError不支持的操作数类型”?

时间:2019-02-03 23:22:56

标签: python python-2.7

我不断遇到错误,而且我不知道为什么索引变成函数类型以及何时运行代码并返回整数。

  

发生异常:exceptions.TypeError不支持/的操作数类型:'int'和'function'

这是我的代码:

def Zero(*eq):
    if len(eq) == 0:
        index = 0
        return index
    else:
        index = eq[0][0]
        k = eq[0][1]
        if k == "+":
            res = index + 0
        elif k == "-":
            res = index - 0
        elif k == "x":
            res = index * 0
        elif k == "/":
            res = 0 / index
        else:
            if index != 0:
                res = 0 // index
        if index == 0:
            error = "Division with zero is invalid"
            return error
        else:
            return res    

def Eight(*eq):
    if len(eq) == 0:
        index = 8
        return index
    else:
        index = eq[0][0]
        k = eq[0][1]
        if k == "+":
            res = index + 8
        elif k == "-":
            res = index - 8
        elif k == "x":
            res = index * 8
        elif k == "/" and index != 0:
            res = 8 / index
        else:
            if index != 0:
                res = 8 // index
        if index == 0:
            error = "Division with zero is invalid"
            return error
        else:
            return res    

def Divide(num):
    k = '/'
    return (num, k)

这是我的执行代码:

x = Zero(Divide(Eight))
print(x)

1 个答案:

答案 0 :(得分:1)

您应该通过在函数对象后加上括号来调用函数Eight;否则,函数对象本身将作为参数传递给Divide

x = Zero(Divide(Eight()))