大家!
我是python编程的初学者。我正在编写一个python程序来反转给定的输入列表。以下是它的代码:
L1=list(input("Enter the numbers of list to be reversed : "))
L2=[]
def rever(La,Lb):
if len(Lb)==0:
return La
else:
return rever(La.append(Lb.pop(0)),Lb)
print rever(L2,L1)
例如,如果我们输入
1,2,3
输出应该是
[3,2,1]
但是,这没有发生。 Python给出以下错误:
Traceback (most recent call last):
File "Q3.py", line 10, in <module>
print rever(L2,L1)
File "Q3.py", line 8, in rever
return rever(La.append(Lb.pop(0)),Lb)
File "Q3.py", line 8, in rever
return rever(La.append(Lb.pop(0)),Lb)
AttributeError: 'NoneType' object has no attribute 'append'
我不明白请帮帮我!!
最佳答案
也许你应该看看这个,而不是弹出和追加
In [5]: L1=list(input("Enter the numbers of list to be reversed : "))
Enter the numbers of list to be reversed : 1,2,3,4,5
In [6]: L1
Out[6]: [1, 2, 3, 4, 5]
In [7]: L2 = L1[::-1]
In [8]: L2
Out[8]: [5, 4, 3, 2, 1]