Python对学生成绩TXT文本数据分析

您所在的位置:网站首页 python统计平均成绩 Python对学生成绩TXT文本数据分析

Python对学生成绩TXT文本数据分析

#Python对学生成绩TXT文本数据分析| 来源: 网络整理| 查看: 265

题目:

从文本文件逐行读入某个班级的百分制成绩序列,计算基本统计值(最大值、最小值、平均值、标准差、中位数),并统计成绩在不同区间([0, 59]、[60,69]、[70,79]、[80,89]、[90,100])的累计数量和百分比。除累计数量外,其他输出保留小数点后两位。并将统计结果存入文本文件中

思路:

主要考察了对python读取文件和写入文件的使用方法

代码: stu = [] def get_file(file_path): fo = open(file_path, "r") while True: line = fo.readline() if len(line) == 0: break stu.append(float(line.strip('\n'))) def calculate(file_path): fo = open(file_path, "a+") stu.sort() fo.write("\n") fo.write("最小值:{}\n".format(round(stu[0]), 2)) stu.sort(reverse=True) fo.write("最大值:{}\n".format(round(stu[0]), 2)) avg = sum(stu)/len(stu) fo.write("平均值:{}\n".format(round(avg, 2))) # 计算标准差 s = 0 for i in stu: s = s + (i-avg)*(i-avg) s = s / len(stu) s = pow(s, 0.5) fo.write("标准差:{}\n".format(round(s, 2))) # 计算中位数 stu.sort() med = stu[len(stu) // 2] if len(stu) % 2 == 0: med = (med + stu[(len(stu)//2) + 1]) / 2 fo.write("中位数:{}\n".format(round(med, 2))) # 计算分数段占比 count = [0, 0, 0, 0, 0] for i in stu: if i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3