为什么用户给出的元素没有被添加到列表中?相反,列表在增加。
list1 =[]
print("How much numbers")
x =int(input())
print("input numbers:")
for n in range(x):
int(input())
y =list1.append(n)
print(list1)
"""
output:
[0,1,2,3]
"""
答案 0 :(得分:1)
您需要将int(input(..))
的值分配给变量,然后将该变量附加到列表list1
上,代码才能正常工作
list1 =[]
print("How much numbers")
x =int(input())
print("input numbers:")
for n in range(x):
#Take input and assign to z
z = int(input())
#Append z to list
y = list1.append(z)
print(list1)
输出将是
How much numbers
4
input numbers:
1
2
3
4
[1, 2, 3, 4]