python - 如何修复“TypeError unsupported operand type(s) for /: 'int' and 'function'”?
问题描述
我一直低于错误,我不知道为什么 index 变成函数类型以及何时运行代码并返回整数。
发生异常: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)
解决方案
您应该Eight
通过在函数对象后面加上括号来调用该函数;否则函数对象本身作为参数传递给Divide
:
x = Zero(Divide(Eight()))
推荐阅读
- multithreading - lock(mutex) 实现通常会尝试确定互斥锁被锁定多长时间以及在哪个核心上?如果不是,为什么不呢?
- html - 如果屏幕更改为像移动设备这样的小屏幕,则将 div 更改为全屏
- c# - 使用c#遍历两个字符串数组并形成单个字符串[]
- python - CVXPY 在 param*norm 的 DCP 规则问题
- python - GAN Discriminator 拒绝生成模型
- r - 在 R data.table 中计算总和的每个变量的聚合
- python - 我不明白为什么 sum(df['series']) != df['series'].sum()
- angular - 一次又一次地从订阅中获取价值
- algorithm - 如何使 Minimax/Alpha-Beta 修剪优先考虑较短的路径?
- python - 了解嵌套函数 Python 3.8 中的执行顺序