Python append_sum(lst)

时间:2018-09-01 20:39:31

标签: python append

请求是

“编写一个名为append_sum的函数,该函数具有一个名为lst的参数。

该函数应将lst的最后两个元素加在一起,并将结果附加到lst。它应该执行三次此过程,然后返回lst。

例如,如果lst以[1、2、3]开始,则最终结果应为[1、1、2、3、5、8]。”

我的代码是:

def append_sum(lst):

    first = lst.append(lst[-1] + lst[-2])

    second = first.append(first[-1] + first[-2])

    third = second.append(second[-1] + second[-2])

    return third

print(append_sum([1, 1, 2]))

然后是错误:
    追溯(最近一次通话):
    
中的文件'script.py',第9行     print(append_sum([1,1,2]))
    文件'script.py',第4行,位于append_sum
    second = first.append(first [-1] + first [-2])
    AttributeError:“ NoneType”对象没有属性
    '追加'

我在哪里错了,为什么?谢谢!

2 个答案:

答案 0 :(得分:0)

这可能会有所帮助。

<form action="http://localhost:8084/mavenFileUpload/rest/upload/image" method="post" enctype="multipart/form-data">
    <input name="file" id="filename" type="file" /><br><br>
    <button name="submit" type="submit">Upload</button>
</form>`

答案 1 :(得分:0)

尝试此代码

 def append_sum(lst, N):
        for i in range(N):
            lst.append(lst[-1] + lst[-2])
        return lst

print(append_sum([1, 1, 2],3))