python使用for循环将用户输入添加到空列表

时间:2018-06-03 17:49:22

标签: python python-3.x python-2.7

所以我想使用此代码将用户输入添加到空列表

no_of_num=int(input('enter the number of numbers you would like to add\n='))#this will store the number of numbers to be added 
list_of_num=[]#this list will store the number to be added
for i in range(0,no_of_num):#we will ask them for the input
    num=int(input('enter the number\n='))
    list_of_num.append(num)#this will keep adding the numbers to the list
result=sum_of_num(num)

但是当我尝试运行此代码时,只需添加用户输入到列表的最后一个数字

1 个答案:

答案 0 :(得分:1)

@Ram Pandey正确添加num,如果i尝试此操作,则可以正常使用

for i in range(0,no_of_num):
    num=int(input('enter the number\n='))
    list_of_num.append(num)
print(sum(list_of_num))