Py_190417_example_5

16 篇文章 0 订阅
12 篇文章 0 订阅
  • get the changeable inputs
#prog1 example_5

#func 1 get input
def getinput():
    nums=[]#define  a viod list
    iput=input("please input numbers (enter for exit):")#for the firstinput 
    while iput !="":
        nums.append(eval(iput))
        iput=input("please input numbers (enter for exit):")#for the while-inputs
    #show the input
    print("the list is")
    for i in nums:
        print(i,end=" ")
    return nums    
#getinput() #test

#func 2 calculate the average(mean) 
def mean(numlist):
    sum=0.0#float for the average calculation 
    for i in numlist:
        sum+=i
        count=len(numlist)
        mean=sum/count
    #print("\nthe mean is {}".format(mean))
    return mean

#func 3 calculate the variance
def variance (vnlist,mean):
    vsum=0.0
    for i in vnlist:
        vsum+=pow(i-mean,2)
        result=pow(vsum/(len(vnlist)-1),0.5)#result=pow(vsum/len(vnlist)-1,0.5)???-1
    #print("\nthe variance is {:.2}".format(result))
    return result
    
#func 3 calculate the median
def median(mlist):
    sorted(mlist)#sort by size of numbers 
    oe=len(mlist)%2#odd-even check
    lenth=len(mlist)
    if oe==0:#even
        median=(mlist[lenth//2-1]+mlist[lenth//2])/2
    else:
        median=mlist[len(mlist)//2]
    #print("the median is ",median)
    return median 
    

def main():
    n=getinput()
    print("\nthe mean is {},the variance is {:.2},the median is {}".format(mean(n),variance (n,mean(n)),median(n)))

#call the func
main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值