python add users input to an empty list using a for loop

回眸只為那壹抹淺笑 提交于 2021-02-17 07:10:12

问题


so I want to add users input to an empty list using this code

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)

but when I try running this code it just add the last number that the user inputs to the list


回答1:


@Ram Pandey is right add num instead if i try this, it works

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))


来源:https://stackoverflow.com/questions/50669304/python-add-users-input-to-an-empty-list-using-a-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!