大家好! 我是 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]
关于python - python程序中出现 "List Reversing"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14974003/