问题
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