关于python往列表添加元素的问题

2024-11-15 20:41:39
推荐回答(2个)
回答(1):

input是Python3中的数据输入命令; 在Python2中,从控制台输入数据是raw_input;
如下所示:

Python 3.2.3 (default, Feb 20 2013, 17:02:41)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = []
>>> for i in range(3):
... a.append(input("Enter: "))
...
Enter: 1 2 3
Enter: 3 4 5
Enter: 5 6 7
>>> a
['1 2 3', '3 4 5', '5 6 7']
>>>

Python 2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = []
>>> for i in range(3):
... a.append(raw_input("Enter: "))
...
Enter: 1 2 3
Enter: 4 5 6
Enter: 7 8 9
>>>
>>> a
['1 2 3', '4 5 6', '7 8 9']
>>>

回答(2):

a.append=input()改

a.append(input())