Python - TypeError: 'int' object is not iterable in simple fucntion

Python - TypeError: 'int' object is not iterable in simple fucntion

a = -7
b = 1
print(abs(a-b))
print(abs(a+b))
sum(a,b)
def sum(i,j):
  return (abs(i+j) == 6) or (abs(i-j) == 6)

错误指出 'int cannot be iterable'

如上面评论中所述,您需要在定义函数后调用函数。此外,也如上面的评论所述,使用与内置函数相同的名称(即 sum)来命名函数并不是最佳做法。

将您的函数重命名为 my_sum,这应该有效:

def my_sum(i,j):
  return (abs(i+j) == 6) or (abs(i-j) == 6)

a = -7
b = 1
print(abs(a-b))
print(abs(a+b))
print(my_sum(a,b))