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
那么我做错了什么?我在任何地方都没有看到任何问题,而且我一直试图将它修复一个小时。
我修复了代码:
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
答案 0 :(得分:1)
你的函数只接受一个参数,而你传递5个参数。替换:
>>>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]
答案 1 :(得分:0)
我很好奇为什么你决定传入列表的文字值而不是传递包含列表的变量。对于前。
x = [1,2,3,4,5]
double_preceding(x)的
这样你的函数应该可以正常工作,这取决于函数内部的代码。你能发布函数内部的实际代码吗?