Python TypeError:接受一个位置参数,但给出了五个
那么我做错了什么?我在任何地方都看不到问题,我已经试着解决了一个小时了 我修正了密码:Python TypeError:接受一个位置参数,但给出了五个,python,typeerror,argument-passing,Python,Typeerror,Argument Passing,那么我做错了什么?我在任何地方都看不到问题,我已经试着解决了一个小时了 我修正了密码: def double_preceding(values): '''(list of ints)->None Update each value in a list with twice the preceding value, and the first item with 0. For example, if x has the value [1,
def double_preceding(values):
'''(list of ints)->None
Update each value in a list with twice
the preceding value, and the first item
with 0.
For example, if x has the value
[1,2,3,4,5], after calling the
double_preceding with argument x,
x would have the value[0,2,4,6,8]
>>>double_preceding(2,3,4,5,6)
[0,4,6,8,10]
>>>double_preceding(3,1,8,.5,10)
[0,6,2,16,1]
'''
if values != []:
temp = values[0]
values[0] = 0
for i in range(0, len(values)):
double = 2 * temp
temp = values[i]
values[i] = double
return #None
您的函数只接受一个参数,而将5个参数传递给它。替换:
def double_preceding(values):
if values != 0:
temp = values[0]
values[0] = 0
for i in range(1, len(values)):
double = 2 * temp
temp = values[i]
values[i] = double
print(values)
return#None
与:
我很好奇为什么您决定传递列表的文本值,而不是传递一个包含列表的变量。例如 x=[1,2,3,4,5] 双_前置(x)
通过这种方式,如果根据函数中的代码传递x,那么函数应该可以工作。你能发布函数中的实际代码吗 所以给了我一个例子来解决这个问题,但给出了一个名为“values”的参数,我可以使用x,这很好,但我仍然得到:函数接受1个位置参数,但给出了5个。所以用x交换值是任意的。如果x!=[]:temp=x[0]x[0]=0表示范围(0,len(x)):double=2*temp=x[i]x[i]=double我算出了。在本例中,我需要添加print(参数)print(值)以获得参数的打印输出。谢谢这是一条非常简洁、明确和清晰的错误信息,不是吗?
>>>double_preceding(2,3,4,5,6)
[0,4,6,8,10]
>>>double_preceding(3,1,8,.5,10)
[0,6,2,16,1]
>>>double_preceding([2,3,4,5,6])
[0,4,6,8,10]
>>>double_preceding([3,1,8,.5,10])
[0,6,2,16,1]