基本统计值计算

“基本统计值计算” 问题分析

1.1 问题分析

基本统计值
  • 需求:给出一组数,对它们有一个概要理解
总个数,求和,平均值,方差,中位数
  • 总个数(len)
  • 求和:for ... in
  • 平均值:求和/总个数
  • 方差:各数据与平均数差的平方的和的平均数
  • 中位数:排序然后 奇数找中间1个,偶数找中间2个取平均

二 “基本统计值计算的实例讲解”

  • 获取多数据输入
  • 通过函数分隔功能
def get_nums():
    """获取数据"""
    nums = []
    num = input('请输入数字:').strip()
    while num != '':
        nums.append(num)
        num = input('请输入数字:').strip()
    return nums
 
 
def get_len(nums):
    """获取长度"""
    count = 0
    for num in nums:
        count += 1
    return count
# nums = get_num()
# print(get_len(nums))
 
 
def get_add(nums):
    """求和"""
    sum = 0
    for num in nums:
        sum += eval(num)
    return sum
 
# nums = get_num()
# print(get_add(nums))
 
def get_mean(nums):
    """求平均数"""
    sum = 0
    for num in nums:
        sum += eval(num)
    mean = sum/get_len(nums)
    return mean
 
# nums = get_nums()
# print(get_mean(nums))
 
def get_var(nums,mean):
    """求方差"""
    sum = 0
    for num in nums:
        sum += (mean-eval(num))**2
    var =   sum / get_len(nums)
    return var
 
# nums = get_nums()
# print(get_var(nums,get_mean(nums)))
 
def get_median(nums):
    """求中位数"""
    nums_sort = sorted(nums)
    size = get_len(nums)
    if size % 2 == 0:
        med = (eval(nums_sort[size // 2 - 1]) + eval(nums_sort[size // 2])) / 2
    else:
        med = eval(nums_sort[size//2])
    return med
nums = get_nums()
print(get_median(nums))
 
优化版的统计值计算
def get_nums():
    """获取数据"""
    nums = []
    num = input('请输入数字:').strip()
    while num != '':
        nums.append(num)
        num = input('请输入数字:').strip()
    return nums
 
 
def get_len(nums):
    """获取长度"""
    count = 0
    for num in nums:
        count += 1
    return count
# nums = get_num()
# print(get_len(nums))
 
 
def get_add(nums):
    """求和"""
    sum = 0
    for num in nums:
        sum += eval(num)
    return sum
 
# nums = get_num()
# print(get_add(nums))
 
def get_mean(nums):
    """求平均数"""
    sum = 0
    for num in nums:
        sum += eval(num)
    mean = sum/get_len(nums)
    return mean
 
# nums = get_nums()
# print(get_mean(nums))
 
def get_var(nums,mean):
    """求方差"""
    sum = 0
    for num in nums:
        sum += (mean-eval(num))**2
    var =   sum / get_len(nums)
    return var
 
# nums = get_nums()
# print(get_var(nums,get_mean(nums)))
 
def get_median(nums):
    """求中位数"""
    nums_sort = sorted(nums)
    size = get_len(nums)
    if size % 2 == 0:
        med = (eval(nums_sort[size // 2 - 1]) + eval(nums_sort[size // 2])) / 2
    else:
        med = eval(nums_sort[size//2])
    return med
# nums = get_nums()
# print(get_median(nums))
 
def run():
    nums = get_nums()
    l1 = ['长度','和','平均值','方差','中位数']
    while True:
        print("欢迎来到数学计算器".center(20,"-"))
        print("""
        1.求长度
        2.求和
        3.求平均值
        4.求方差
        5.求中位数
        6:退出程序
        """)
        choice = input("请输入你的需求:").strip()
        result = 0
        if choice == '1':
            result = get_len(nums)
        elif choice == '2':
            result = get_add(nums)
        elif choice == '3':
            result = get_mean(nums)
        elif choice == '4':
            result = get_var(nums,get_mean(nums))
        elif choice == '5':
            result = get_median(nums)
        elif choice == '6':
            return
        else:
            print('输入格式有误')
            print(result)
        print(f"{l1[eval(choice) -1]}:{result}")
 
run()
 

posted on 2019-10-25 20:10  Everuse  阅读(1013)  评论(0编辑  收藏  举报

导航