基本统计方法
通过用户输入一组数据(用户一个一个输入),然后计算数据的中位数/平均数/方差/求和
nums = []
while True:
num1 = input("请输入数字(按Q退出):")
if num1 == 'Q':
break
nums.append(int(num1))
# 中位数
def get_median(nums):
nums.sort()
nums_len = len(nums)
if nums_len % 2 == 0:
print("中位数:",(nums[int(nums_len / 2 - 1)] + nums[int(nums_len / 2)])/2)
else:
print("中位数:",nums[nums_len // 2])
get_median(nums)
# 求和
def get_sum(nums):
count = 0
for i in nums:
count += int(i)
return count
count = get_sum(nums)
print('和:',count)
# 平均数
def get_average(nums):
count = get_sum(nums)
average_count = count/len(nums)
return average_count
average_count = get_average(nums)
print("平均数:",average_count)
# 方差
def get_variance(nums):
average_count = get_average(nums)
variance_count = 0
for i in nums:
variance_count += pow(i-average_count,2)
return variance_count
variance_count = get_variance(nums)
print("方差:",variance_count)