预科第五天

预科第五天

基本统计值计算

def get_nums():
   nums = []   #定义一个列表
   num = input("请输入数字:").strip()
   while num != "":
       nums.append(num)
       num = input("请输入数字:").strip()
   return nums



def get_len(nums):
   #获取长度
   count = 0
   for i in nums:
       count += 1
   return count


def get_add(nums):
    #求和
   sum = 0
   for num in nums:
       sum += eval(num)
   return sum

def get_mean(nums):
   #求平均数
   mean = get_add(nums)/get_len(nums)
   return mean

def get_fc(nums,mean):
   #求方差
   sum = 0
   for num in nums:
       sum += (mean-eval(num))**2
   fc = sum/get_len(nums)
   return nums

l1 =[1,2,3,4,5,6]
def get_median(nums):
   #求中位数
   nums_sort = sorted(nums) #从小到大排序

# nums =get_nums()
# print(get_median(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()
   l2 =['长度','和','平均数','方差','中位数']
   while True:
       print('欢迎来到数学计算机',(20,'-'))
       print("""
      1、求长度
      2、求和
      3、求平均数
      4、求方差
      5、求中位数""")

       choice ='请输入您的需求'
       if choice == 1:
           print(get_len(nums))
       elif choice == 2:
           print(get_len(nums))

 

猜你喜欢

转载自www.cnblogs.com/baohanblog/p/11740924.html