Python学生成绩管理系统(源码)

代码教程:

项目介绍

源码:

class Student:
    def __init__(self, id, name, age):
        self.id = id
        self.name = name
        self.age = age


class Score:
    def __init__(self, student_id, subject, score):
        self.student_id = student_id
        self.subject = subject
        self.score = score


class StudentManagement:
    def __init__(self):
        self.students = []

    def add_student(self, student):
        self.students.append(student)

    def update_student(self, student):
        for s in self.students:
            if s.id == student.id:
                s.name = student.name
                s.age = student.age
                break

    def delete_student(self, id):
        self.students = [s for s in self.students if s.id != id]

    def query_student(self, id):
        for student in self.students:
            if student.id == id:
                return student
        return None


class ScoreManagement:
    def __init__(self):
        self.scores = []

    def enter_scores(self, score):
        self.scores.append(score)

    def update_scores(self, score):
        for s in self.scores:
            if s.student_id == score.student_id and s.subject == score.subject:
                s.score = score.score
                break

    def delete_scores(self, student_id, subject):
        self.scores = [s for s in self.scores if not (s.student_id == student_id and s.subject == subject)]


class ScoreQuery:
    def __init__(self, score_management):
        self.score_management = score_management

    def query_by_id(self, id):
        return [score for score in self.score_management.scores if score.student_id == id]

    def query_by_name(self, name):
        # 假设通过学生管理对象获取学生 ID
        student_ids = [student.id for student in self.score_management.student_management.students if
                       student.name == name]
        return [score for score in self.score_management.scores if score.student_id in student_ids]

    def class_scores(self):
        return self.score_management.scores


class ScoreStatistics:
    def __init__(self, score_management):
        self.score_management = score_management

    def calculate_avg(self):
        total_score = 0
        count = 0
        for score in self.score_management.scores:
            total_score += score.score
            count += 1
        return total_score / count if count > 0 else 0
    def high_low(self):
        scores = [score.score for score in self.score_management.scores]
        return max(scores), min(scores)

    def fail_rate(self):
        fail_count = 0
        for score in self.score_management.scores:
            if score.score < 60:
                fail_count += 1
        return fail_count / len(self.score_management.scores) if len(self.score_management.scores) > 0 else 0

    def excellent_rate(self):
        excellent_count = 0
        for score in self.score_management.scores:
            if score.score >= 90:
                excellent_count += 1
        return excellent_count / len(self.score_management.scores) if len(self.score_management.scores) > 0 else 0


class Menu:
    def __init__(self, student_management, score_management, score_query, score_statistics):
        self.student_management = student_management
        self.score_management = score_management
        self.score_query = score_query
        self.score_statistics = score_statistics

    def display_menu(self):
        print("=" * 30)
        print("学生成绩管理系统")
        print("1. 添加学生")
        print("2. 更新学生信息")
        print("3. 删除学生")
        print("4. 查询学生")
        print("5. 录入成绩")
        print("6. 更新成绩")
        print("7. 删除成绩")
        print("8. 按学号查询成绩")
        print("9. 按姓名查询成绩")
        print("10. 查询班级成绩")
        print("11. 计算平均分")
        print("12. 查找最高分和最低分")
        print("13. 计算不及格率")
        print("14. 计算优秀率")
        print("15. 展示所有学生信息")
        print("16. 展示所有成绩信息")
        print("0. 退出")
        print("=" * 30)

    def handle_choice(self):
        while True:
            self.display_menu()
            choice = int(input("请输入您的选择: "))
            if choice == 0:
                break
            elif choice == 1:
                id = int(input("请输入学号: "))
                name = input("请输入姓名: ")
                age = int(input("请输入年龄: "))
                student = Student(id, name, age)
                self.student_management.add_student(student)
            elif choice == 2:
                id = int(input("请输入要更新的学生学号: "))
                new_name = input("请输入新姓名: ")
                new_age = int(input("请输入新年龄: "))
                new_student = Student(id, new_name, new_age)
                self.student_management.update_student(new_student)
            elif choice == 3:
                id = int(input("请输入要删除的学生学号: "))
                self.student_management.delete_student(id)
            elif choice == 4:
                id = int(input("请输入要查询的学生学号: "))
                student = self.student_management.query_student(id)
                if student:
                    print(f"学号: {student.id}, 姓名: {student.name}, 年龄: {student.age}")
                else:
                    print("未找到该学生")
            elif choice == 5:
                student_id = int(input("请输入学生学号: "))
                subject = input("请输入科目: ")
                score = float(input("请输入成绩: "))
                score_obj = Score(student_id, subject, score)
                self.score_management.enter_scores(score_obj)
            elif choice == 6:
                student_id = int(input("请输入学生学号: "))
                subject = input("请输入科目: ")
                new_score = float(input("请输入新成绩: "))
                new_score_obj = Score(student_id, subject, new_score)
                self.score_management.update_scores(new_score_obj)
            elif choice == 7:
                student_id = int(input("请输入学生学号: "))
                subject = input("请输入科目: ")
                self.score_management.delete_scores(student_id, subject)
            elif choice == 8:
                id = int(input("请输入学号: "))
                scores = self.score_query.query_by_id(id)
                for score in scores:
                    print(f"学号: {score.student_id}, 科目: {score.subject}, 成绩: {score.score}")
            elif choice == 9:
                name = input("请输入姓名: ")
                scores = self.score_query.query_by_name(name)
                for score in scores:
                    print(f"学号: {score.student_id}, 科目: {score.subject}, 成绩: {score.score}")
            elif choice == 10:
                scores = self.score_query.class_scores()
                for score in scores:
                    print(f"学号: {score.student_id}, 科目: {score.subject}, 成绩: {score.score}")
            elif choice == 11:
                avg = self.score_statistics.calculate_avg()
                print(f"平均分: {avg}")
            elif choice == 12:
                high, low = self.score_statistics.high_low()
                print(f"最高分: {high}, 最低分: {low}")
            elif choice == 13:
                fail_rate = self.score_statistics.fail_rate()
                print(f"不及格率: {fail_rate}")
            elif choice == 14:
                excellent_rate = self.score_statistics.excellent_rate()
                print(f"优秀率: {excellent_rate}")
            elif choice == 15:
                students = self.student_management.students
                for student in students:
                    print(f"学号: {student.id}, 姓名: {student.name}, 年龄: {student.age}")
            elif choice == 16:
                scores = self.score_management.scores
                for score in scores:
                    print(f"学号: {score.student_id}, 科目: {score.subject}, 成绩: {score.score}")

if __name__ == "__main__":

    # 初始化学生管理对象、成绩管理对象、成绩查询对象和成绩统计对象
    student_management = StudentManagement()
    score_management = ScoreManagement()
    score_query = ScoreQuery(score_management)
    score_statistics = ScoreStatistics(score_management)
    menu = Menu(student_management, score_management, score_query, score_statistics)

    # 测试数据:添加学生
    student1 = Student(1, "张三", 18)
    student2 = Student(2, "李四", 19)
    student3 = Student(3, "王五", 20)
    student4 = Student(4, "赵六", 21)
    student5 = Student(5, "孙七", 22)
    student_management.add_student(student1)
    student_management.add_student(student2)
    student_management.add_student(student3)
    student_management.add_student(student4)
    student_management.add_student(student5)

    # 测试数据:录入成绩
    score1 = Score(1, "数学", 85)
    score2 = Score(1, "英语", 78)
    score3 = Score(1, "物理", 92)
    score4 = Score(2, "数学", 90)
    score5 = Score(2, "英语", 88)
    score6 = Score(2, "化学", 85)
    score7 = Score(3, "数学", 75)
    score8 = Score(3, "英语", 68)
    score9 = Score(3, "生物", 80)
    score10 = Score(4, "数学", 88)
    score11 = Score(4, "英语", 76)
    score12 = Score(4, "政治", 90)
    score13 = Score(5, "数学", 95)
    score14 = Score(5, "英语", 82)
    score15 = Score(5, "历史", 88)
    score_management.enter_scores(score1)
    score_management.enter_scores(score2)
    score_management.enter_scores(score3)
    score_management.enter_scores(score4)
    score_management.enter_scores(score5)
    score_management.enter_scores(score6)
    score_management.enter_scores(score7)
    score_management.enter_scores(score8)
    score_management.enter_scores(score9)
    score_management.enter_scores(score10)
    score_management.enter_scores(score11)
    score_management.enter_scores(score12)
    score_management.enter_scores(score13)
    score_management.enter_scores(score14)
    score_management.enter_scores(score15)


    menu.handle_choice()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值