首页 > 编程语言 > 详细

python-demo:计算合格率

时间:2020-04-28 18:12:25      阅读:116      评论:0      收藏:0      [点我收藏+]

计算合格率:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

# 定义计算方法
def calcPercent(inStr):
    try:
        listArr = inStr.split(,)
        listArr = map(eval, listArr)
        sumCount = len(listArr)

        newList = list(filter(lambda item : item >= baseNumber, listArr));
        countNewList = len(newList)

        return {:.2%}.format(float(countNewList) / float(sumCount), ".1f")
    except:
        return "请输入正确的数字字符串,以‘,‘分隔"


# 基础数
baseNumber = 250
# 输入字符串
#str = ‘345,231,543,280,123,789,456,12‘
str = ‘‘
# 调用方法
print(calcPercent(str))

 

高级写法:

std = 250

def main(n, sc_list):
    q_list = list(filter(lambda x: x > std, sc_list))
    return len(q_list) / n

if __name__ == "__main__":
    inputs = input("Input the info:")
    input_list = [float(i) for i in inputs.split()]
    input_n = int(input_list[0])
    input_sc_list = input_list[1:]
    if input_n != len(input_sc_list):
        print("Lenth not right")
    print(main(input_n, input_sc_list))

 

python-demo:计算合格率

原文:https://www.cnblogs.com/betx/p/12795699.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!