在计算中位数时,我在python 3中遇到了所有测试用例的运行时错误

时间:2018-09-16 06:02:43

标签: python-3.x

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

1 个答案:

答案 0 :(得分:0)

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

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