import math
def getNum(ls):
n=len(ls)
return n
def mean(numbers):
s=0
for i in ls:
i=float(i)
s=s+i
aver=s/numbers
return aver
def dev(numbers, mean):
a=0
for i in ls:
i=float(i)
a=a+(i-mean)**2
b=math.sqrt(float(a/numbers))
return b
def median(numbers):
ls2=sorted(ls)
c=0
if numbers%2==0:
k=numbers/2
for i in ls2[k,k+2]:
i=float(i)
c=c+i
return c/2
else:
k=(numbers+1)/2
k=int(k)
z=ls2[k-1]
z=int(z)
return z
m=input()
ls=[]
ls=m.split(",")
n=getNum(ls)
aver=mean(n)
q=dev(n,aver)
k=median(n)
print("Average:{:.2f},Standard Deviation:{:.2f},Median:{}".format(aver,q,k))
Traceback (most recent call last):
File "/545401477/p545401477", line 49, in <module>
k=median(n)
File "/545401477/p545401477", line 32, in median
for i in ls2[k,k+2]:
TypeError: list indices must be integers or slices, not tuple
上述错误有时会出现,而且当我输入1,3,6,9,2,5,1
错误输出:
Average:3.86,Standard Deviation:2.75,Median:3
期望输出:
Average:3.86,Standard Deviation:2.97,Median:3