用Python从文件中读取学生成绩,并计算最高分/最低分/平均分
2023-03-31 10:36:25 时间
兄弟们,今天咱们试试用Python从文件中读取学生成绩,并计算最高分/最低分/平均分。
涉及知识点
- 文件读写
- 基础语法
- 字符串处理
- 循环遍历
代码展示
模块
import platform # 我还给大家准备了这些资料:Python视频教程、100本Python电子书、基础、爬虫、数据分析、web开发、机器学习、人工智能、面试题、Python学习路线图、问题解答! # 都放在这个扣群啦:279199867
定义获取最高分、最低分及平均分函数
def compute_score(): scores = [] with open("./py023.txt", encoding="utf8") as fin: for line in fin: line = line.strip() fields = line.split(",") scores.append(int(fields[-1])) max_score = max(scores) min_score = min(scores) avg_score = round(sum(scores) / len(scores), 2) return max_score, min_score, avg_score
调用函数
max_score, min_score, avg_score = compute_score() print("最高分:" + str(max_score) + " " + "最低分:" + str(min_score) + " " + "平均分:" + str(avg_score))
全部代码
# 导入系统包 import platform print("待到红旗满天下,马踏东京赏樱花。富士山上扬汉旗,樱花树下醉胡姬。") print("Python从文件中读取学生成绩,并计算最高分/最低分/平均分 ") # 定义获取最高分、最低分及平均分函数 def compute_score(): scores = [] with open("./py023.txt", encoding="utf8") as fin: for line in fin: line = line.strip() fields = line.split(",") scores.append(int(fields[-1])) max_score = max(scores) min_score = min(scores) avg_score = round(sum(scores) / len(scores), 2) return max_score, min_score, avg_score # 调用函数 max_score, min_score, avg_score = compute_score() print("最高分:" + str(max_score) + " " + "最低分:" + str(min_score) + " " + "平均分:" + str(avg_score)) print("Python 版本", platform.python_version())
相关文章
- Python中的函数与方法 以及Bound Method和Unbound Method
- 一文贯通python文件读取
- Python 中的异步编程:Asyncio
- 7个你现在就该学习Python的理由
- 提高Python运行效率的六个窍门
- Python数据科学:神经网络
- 一篇文章看懂大数据分析就业前景及职能定位
- R和Python中的文本挖掘:8个入门小贴士
- 告诉你为什么Python有点慢,但我却无所谓?
- 专注学习DevOps编程语言Top 5推荐
- Python发送邮件脚本
- Python多进程并行编程实践: mpi4py 的使用
- Python语言在未来的发展前景
- Python vs Ruby: 谁是最好的 web 开发语言?
- Python对Ruby:谁在Web开发领域更胜一筹?
- Python一行代码完成并行任务
- Python开发者2017应该关注的七个类库
- python爬虫入门基本知识
- 在终端中优雅地编写Python
- Python机器学习实战:信用卡欺诈检测