500字范文,内容丰富有趣,生活中的好帮手!
500字范文 > 26.读入文件数据 统计学生成绩最高分 最低分 平均分

26.读入文件数据 统计学生成绩最高分 最低分 平均分

时间:2023-01-31 23:02:09

相关推荐

26.读入文件数据 统计学生成绩最高分 最低分 平均分

#输入文件:

#三列:学号,姓名,成绩(用逗号隔开) ,行之间用\n换行分割

#输出:最高分,最低分,平均分

def computer_score():score = []with open("./student_score") as fin: # 读取文件student_scorefor line in fin:fields = line[:-1] # 以切片的方式去除{}score.append(int(fields[-1]))max_score = max(score)min_score = min(score)avg_score = round(sum(score) / len(score), 2) # 求平均分并用round(,2)保留两位小数return max_score, min_score, avg_scoremax_score, min_score, avg_score = computer_score() # 获取三个函数变量print(f"最高分为:={max_score}, 最低分为:={min_score}, 平均分为:={avg_score}") # 加f可以直接使用={ }进行打印变量

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。