# CalStatisticsV1.py
def getNum():
nums = []
numsstr = input("请输入数字(回车退出):")
while numsstr != "":
nums.append(eval(numsstr))
numsstr = input("请输入数字(回车退出):")
return nums
def mean(numbers):
s = 0.0
for num in numbers:
s += num
return s / len(numbers)
def dev(numbers, mean):
sdev = 0.0
for num in numbers:
sdev += (num - mean) ** 2
return pow(sdev / (len(numbers) - 1), 0.5)
def median(numbers):
numbers.sort()
if len(numbers) % 2 == 0:
med = (numbers[len(numbers)//2-1] + numbers[len(numbers)//2]) / 2
else:
med = numbers[len(numbers)//2]
return med
n = getNum()
m = mean(n)
print("平均值:{},方差:{},中位数:{}".format(m, dev(n, m), median(n)))
PS: source, python123