python-3.x - 在计算中位数时,python 3 中的所有测试用例都出现运行时错误
问题描述
# Enter your code here. Read input from STDIN. Print output to STDOUT
from statistics import median
a=str(input())
l=a.split(' ')
for i,v in enumerate(l):
l[i]=float(v)
print('%.9f'%median(l))
解决方案
尝试进行一些数据验证来检查错误,
# Enter your code here. Read input from STDIN. Print output to STDOUT
from statistics import median
a = str(input('Enter the series of values - '))
try:
l = [float(value.strip()) for value in a.split(' ') if value.strip()]
except Exception as error:
print('Error while float conversion - {}'.format(error))
l = []
if l:
print('%.9f' % median(l))
else:
print('Enter valid inputs.')
推荐阅读
- c# - 按下另一个键后,该键不再被视为“按下”
- c# - 如何修复我的代码以将已经存在的文件移动到另一个目录?
- python - 使用 pandas 的 sql 命令“OVER PARTITION”时出错
- c - C 文件中的结构数未知
- elasticsearch - 在弹性搜索中搜索即时创建的时间戳
- c++ - 为什么 std::strlen() 在不终止空字符的情况下对 char 数组起作用?这是编译器优化吗?
- javascript - 如何创建显示隐藏按钮和所有按钮名称的幻灯片?
- python - 重载 changelist_view 而没有导致错误的更改
- tsql - 这个 70-761 考试转储问题背后的原因是什么
- python - 使用python在html中查找断行