odd_numbers() takes 0 positional arguments but 1 was given
时间: 2023-05-16 11:05:27 浏览: 53
这个问题是一个编程类的问题,它意味着 odd_numbers() 函数需要 0 个位置参数,但是却传入了 1 个参数。这可能是因为在函数调用时传入了一个参数,但是函数定义中没有指定参数。或者,函数定义中指定了参数,但是在函数调用时没有传入参数。需要检查函数定义和函数调用的代码,以确保它们匹配。
相关问题
a = [1, 10, 9, 7, 2, 3] b = [1, 3, 7, 9] def odd_numbers(list_arg): for item in list_arg: new_list = [] if ((item % 2) != 0): new_list.append(item) return sorted(new_list, reverse = False) odd_numbers(a)为什么只输出一个值
这是因为在for循环中,每次都会重新创建一个空列表`new_list`,因此只有最后一个奇数被添加进去并进行了排序。要解决这个问题,可以将`new_list`的创建放在for循环外部,这样就可以将所有的奇数都添加进去并排序。修改后的代码如下:
```
a = [1, 10, 9, 7, 2, 3]
b = [1, 3, 7, 9]
def odd_numbers(list_arg):
new_list = []
for item in list_arg:
if ((item % 2) != 0):
new_list.append(item)
return sorted(new_list, reverse = False)
print(odd_numbers(a)) # 输出 [1, 3, 7, 9]
```
def count_odd_even(s): odd_sum = 0 even_sum = 0 for c in s: if c.isdigit(): if int(c) % 2 == 0: even_sum += int(c) else: odd_sum += int(c) return odd_sum, even_sum s=input() print('oddsum={},evensum={}'.format(s))
这段代码有一些问题,我来解释一下。
首先,在 `print` 语句中的 `format` 方法的参数应该是 `count_odd_even(s)`,而不是 `s`。因为 `count_odd_even(s)` 才是函数返回的元组,包含了奇数和偶数的和。
其次,在 `format` 方法中,应该使用 `{}` 作为占位符,而不是 `{}` 中指定的字符串。
最后,在输出语句中,应该将返回的奇数和偶数的和分别放到 `oddsum` 和 `evensum` 中,而不是反过来。
所以,修正后的代码应该是这样的:
```
def count_odd_even(s):
odd_sum = 0
even_sum = 0
for c in s:
if c.isdigit():
if int(c) % 2 == 0:
even_sum += int(c)
else:
odd_sum += int(c)
return odd_sum, even_sum
s = input()
oddsum, evensum = count_odd_even(s)
print('oddsum={}, evensum={}'.format(oddsum, evensum))
```