python使用for循环将用户输入添加到空列表中
因此,我想使用以下代码将用户输入添加到空列表中python使用for循环将用户输入添加到空列表中,python,python-3.x,python-2.7,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 as
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)
但当我尝试运行此代码时,它只需将用户输入的最后一个数字添加到列表中@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))
为什么您要将您的区域的运行编号添加到列表中,而不是用户输入的编号?抱歉,我不明白您正在添加
i
notnum
我也尝试了,但它不起作用,所以我尝试了num