python递归功能逆转列表
I want to write a recursive function that reverses a list.
Given an input: [1,2,3], the function should return [3,2,1]
I am however recieving this error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这样:
输出:
Try like this :
Output:
无需任何递归编程:
No need for any recursive programming:
newlist.append(temp)
没有返回值,因此返回无。newlist.append(temp)
将温度添加到newlist,因此您的代码可以工作为:newList.append(temp)
doesn't have a return value, and therefore returns None.newList.append(temp)
adds temp to newList, so your code could work as:从我在错误消息中看到的错误,您正在尝试将某些类型附加到line
返回yourlist.append(element)
中的内容。尝试首先附加到列表中,然后返回。
喜欢
From what I can see in the error message it is throwing an error is you are trying to append something to none type as in the line
return yourlist.append(element)
.Try appending to the list first and then return it.
Like
在Python中,列表是可变的对象,
List.Append
将元素添加到现有列表中,但不会返回任何内容。这就是为什么您会遇到错误。In python, lists are mutable objects, and
list.append
adds an element to the existing list but doesn't return anything. That's why you're getting that error.