python使用for循环将用户输入添加到空列表 [英] python add users input to an empty list using a for loop
本文介绍了python使用for循环将用户输入添加到空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我想使用此代码将用户输入添加到空列表
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
推荐答案
@Ram Pandey 是正确的添加 num
代替,如果 i
试试这个,它可以工作
@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))
这篇关于python使用for循环将用户输入添加到空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文