#CalStatisticsV1.py
defgetNum(): #获取用户不定长度的输入
nums =[]
iNumStr =input("请输入数字(回车退出): ")
whileiNumStr !="":
nums.append(eval(iNumStr))
iNumStr =input("请输入数字(回车退出): ")
returnnums
defmean(numbers): #计算平均值
s =0.0
fornum innumbers:
s =s +num
returns /len(numbers)
defdev(numbers, mean): #计算方差
sdev =0.0
fornum innumbers:
sdev =sdev +(num -mean)**2
returnpow(sdev /(len(numbers)-1), 0.5)
defmedian(numbers): #计算中位数
sorted(numbers)
size =len(numbers)
ifsize %2==0:
med =(numbers[size//2-1] +numbers[size//2])/2
else:
med =numbers[size//2]
returnmed
n =getNum() #主体函数
m =mean(n)
print("平均值:{},方差:{:.2},中位数:{}.".format(m, dev(n,m),median(n)))