用Python从文件中读取学生成绩,并计算最高分/最低分/平均分

兄弟们,今天咱们试试用Python从文件中读取学生成绩,并计算最高分/最低分/平均分。

涉及知识点

  • 文件读写
  • 基础语法
  • 字符串处理
  • 循环遍历

代码展示

模块

import platform

我还给大家准备了这些资料:Python视频教程、100本Python电子书、基础、爬虫、数据分析、web开发、机器学习、人工智能、面试题、Python学习路线图、问题解答!
都放在这个扣群啦:279199867

定义获得最高分、最低分和平均分的函数

[En]

Define the function of obtaining the highest, lowest and average scores

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) +
      "\n" + "最低分:" + str(min_score) +
      "\n" + "平均分:" + str(avg_score))

全部代码

导入系统包
import platform

print("待到红旗满天下,马踏东京赏樱花。富士山上扬汉旗,樱花树下醉胡姬。")
print("Python从文件中读取学生成绩,并计算最高分/最低分/平均分 \n")

定义获得最高分、最低分和平均分的函数<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Define the function of obtaining the highest, lowest and average scores</font>*</details>
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) +
      "\n" + "最低分:" + str(min_score) +
      "\n" + "平均分:" + str(avg_score))

print("Python 版本", platform.python_version())

Original: https://www.cnblogs.com/hahaa/p/16520970.html
Author: 轻松学Python
Title: 用Python从文件中读取学生成绩,并计算最高分/最低分/平均分

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/498981/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球