计算合格率:
#!/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))