给出一组数据,对它们进行总个数、求和、平均值、方差、中位数计算。
def getNum():
nums = []
i = input("请输入数字(回车退出:)")
while i != '':
nums.append(eval(i))
i = input("请输入数字(回车退出:)")
return nums
def avrage(nums):
sum = 0
for item in nums:
sum += item
return sum/(len(nums))
def dev(nums,avr):
sum = 0
for item in nums:
sum += (item - avr)**2
return pow(sum/(len(nums)-1),0.5)
def median(nums):
sorted(nums)
i = len(nums)
if i%2 == 0:
mid = (nums[i//2-1] + nums[i//2])/2
else:
mid = nums[i//2]
return mid
n = getNum()
m = avrage(n)
print("平均值:{},方差:{:.2},中位数:{}".format(m,dev(n,m),median(n)))