python一行输入n个数据问题
作者:圣诞节不感冒 时间:2023-09-11 21:50:48
python一行输入n个数据
有时会碰到一行输入多个数据,这是可以先用str类型存一组数据,然后再迭代的将每个数据追加到新的列表中。
方法一
先输入str字符串,再根据空格得到数字字符列表n.split().然后将列表元素转为数字存到新的列表list中
上面的问题是要输入10个数据,其实也能输入任意个。
n = input()
list = []
for i in n.split():
list.append(int(i))
for i in list:
print(i)
方法二
方法一的缩写
n = input()
list = [int(j) for j in n.split()]
print(list)
或则再缩写成下面
list= [int(num) for num in input().split()]
print(list)
输入之后,打印结果如下:
输入N个数,求其平均值
count = 0
sum = 0
while True:
a = int(input())
sum=sum + a
count += 1
print(sum/count)
采用了while无限循环的语句,不然不知道加多少次。count是用来计数的。
这个样子写就比较好了
count = 0
sum = 0
while True:
a = int(input())
if a =="quit":
break
sum=sum + a
count += 1
print(sum/count)
来源:https://blog.csdn.net/stay_zezo/article/details/103207530
标签:python,输入,数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一个基于flask的web应用诞生 flask和mysql相连(4)
2024-01-18 20:37:16
![](https://img.aspxhome.com/file/2023/6/124286_0s.jpg)
RabbitMq如何做到消息的可靠性投递
2024-04-23 09:36:57
![](https://img.aspxhome.com/file/2023/2/134402_0s.png)
Python+OpenCV实现基于颜色的目标识别
2022-10-28 02:57:27
![](https://img.aspxhome.com/file/2023/2/93592_0s.jpg)
MySQL数据库配置优化的方案
2024-01-14 00:41:52
在ASP应用程序中加入智能搜索
2007-09-18 13:15:00
正确的PHP匹配UTF-8中文的正则表达式
2024-04-10 10:56:36
Go 语言结构体链表的基本操作
2024-02-07 18:51:11
![](https://img.aspxhome.com/file/2023/4/101044_0s.png)
一道关于数据库(经典父子级 ID 关联)更新题
2024-01-17 00:07:19
![](https://img.aspxhome.com/file/2023/3/124853_0s.png)
Mysql服务添加 iptables防火墙策略的方案
2024-01-21 14:44:34
如何设置mysql允许外网访问
2024-05-03 15:49:38
Gin golang web开发模型绑定实现过程解析
2024-04-25 15:10:59
用Eclipse写python程序
2022-03-24 02:24:25
![](https://img.aspxhome.com/file/2023/5/101435_0s.jpg)
django连接oracle时setting 配置方法
2021-08-23 15:20:13
详解Python中Pyyaml模块的使用
2021-08-19 08:18:23
![](https://img.aspxhome.com/file/2023/0/99150_0s.png)
Python编程实现简易的音乐播放器基本操作
2022-05-25 08:41:16
![](https://img.aspxhome.com/file/2023/5/80685_0s.jpg)
Vue + Webpack + Vue-loader学习教程之相关配置篇
2024-04-29 13:10:40
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
2023-11-18 01:11:16
python办公自动化之excel的操作
2023-11-20 14:01:01
FrontPage XP设计制作网页小技巧八则
2008-06-04 12:43:00
Python图像处理之膨胀与腐蚀的操作
2022-10-07 19:47:06
![](https://img.aspxhome.com/file/2023/8/117618_0s.png)