首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python使用for循环将用户输入添加到空列表中

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

Stack Overflow用户
提问于 2018-06-04 01:49:22
回答 1查看 453关注 0票数 -1

因此,我想使用以下代码将用户输入添加到一个空列表中

代码语言:javascript
复制
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)

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

EN

回答 1

Stack Overflow用户

发布于 2018-06-04 01:54:28

@Ram Pandey是对的,如果i尝试这样做,添加num,它是有效的

代码语言:javascript
复制
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))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50669304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档