首页 > 解决方案 > 在计算中位数时,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))

标签: python-3.x

解决方案


尝试进行一些数据验证来检查错误,

# 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.')

推荐阅读