为什么 sum(*args)int' object is not callable
时间: 2023-07-17 13:56:05 浏览: 67
我犯了一个错误,非常抱歉给你带来困惑。`sum()` 函数不需要使用 `*args` 语法来接收可变数量的参数。实际上,`sum()` 函数本身就接受一个可迭代对象作为参数,并计算其总和。
因此,你只需要将整个可迭代对象作为参数传递给 `sum()` 函数即可。不需要使用 `*args`。以下是修复后的代码:
```python
def number(args):
average = float(sum(args) / len(args))
result_list = []
for i in args:
if i > average:
result_list.append(i)
result_tuple = (average, result_list)
return result_tuple
print(number([4, 5, 9, 3, 5]))
```
在修复后的代码中,我们重新定义了 `number` 函数,接受一个列表 `args` 作为参数。然后我们直接将 `args` 传递给 `sum()` 函数来计算总和。接下来,我们按照原来的逻辑计算平均值,并将大于平均值的数字添加到结果列表中。最后,我们返回一个包含平均值和结果列表的元组。
非常抱歉给你带来混淆,感谢你指出错误。
相关问题
decorator TypeError: 'int' object is not callable
"TypeError: 'NoneType' object is not callable" 是一个常见的错误消息,它通常发生在你试图调用一个没有返回值的函数时。在你提供的代码示例中,出现这个错误的原因是在装饰器函数的定义中,你错误地在返回语句中加上了括号,导致装饰器返回的是 NoneType 对象而不是函数对象。
要解决这个问题,只需将装饰器函数的返回语句中的括号去掉即可。例如,在第二个装饰器函数中,将 `return out()` 改为 `return out`。同样,在第一个装饰器函数中,也需要将 `return wrapper()` 改为 `return wrapper`。
修正后的代码示例如下:
```python
# 装饰器定义
def decorator(func):
def wrapper(*args, **kwargs):
start_time = time.time()
func(*args, **kwargs)
end_time = time.time()
timer = end_time - start_time
print("run time spend :", timer, 's', sep='')
return wrapper # 去掉括号即可解决问题
# 被装饰的函数
@decorator
def run_func():
a = int(input("input x:").strip(' '))
b = int(input("input y:").strip(' '))
print("%d ** %d = %d" % (a, b, a ** b))
# 调用被装饰的函数
run_func()
```
通过这个修改,你应该能够成功运行你的代码,而不会再出现 "TypeError: 'NoneType' object is not callable" 的错误。
intersect TypeError: 'int' object is not callable
根据提供的引用内容,我们可以看出,函数`intersect()`是一个用于求取多个集合的交集的函数。它的定义是在引用中给出的,它接受任意数量的参数,并返回这些参数的交集。
然而,当我们调用`intersect()`函数时,如果我们传递给它的参数中包含了一个整数类型的对象,就会出现`TypeError: 'int' object is not callable`的错误。这是因为在函数内部使用了类似于调用函数的语法,导致解释器错误地将整数对象当作可调用的函数来处理。
要解决这个错误,我们需要检查我们传递给`intersect()`函数的所有参数,确保它们都是可迭代的对象(如列表、元组、字符串等),而不是整数类型的对象。这样就可以避免出现`TypeError`错误。
以下是修复`intersect()`函数的建议方法:
1. 在函数的开头添加一个`if`语句,用于判断如果参数中包含整数类型的对象,则返回一个空列表或者抛出一个异常。
2. 使用`isinstance()`函数来检查每个参数是否是可迭代的对象。如果参数不是可迭代的对象,则返回一个空列表或者抛出一个异常。
修复后的`intersect()`函数定义如下:
```python
def intersect(*args):
res = []
for x in args
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)