python - python使用for循环将用户输入添加到空列表
问题描述
所以我想使用此代码将用户输入添加到一个空列表
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))
推荐阅读
- java - 如何修复 org.hibernate.PropertyAccessException:无法通过反射设置器设置字段值
- grpc - 符号数据库中的消息名称冲突。如果我没有在符号数据库中注册消息会怎样?
- azure-media-services - 如何通过 REST API 获取 Azure 媒体服务中的视频缩略图
- python - 如果虚拟变量之一的得分为 0.95,则分类变量是否相关?
- c# - POST 值从 winform 到 aspx webform
- android - 当我包含一些“.h”文件时如何制作“Android.bp”
- sql - 当我使用 SQL Navigator 时,我的查询正在运行,但是当我从前端调用时,我的查询返回 0
- azure - 如何在不使用线程的情况下在拉取请求中创建评论
- mysql - “'true' as QUERYID”是什么意思?
- wordpress - 从登台转移到实时服务器后,WordPress 网站无法正常工作