[Python急救站]簡單的學生管理系統

Jinylin發表於2024-03-13

簡單的學生管理系統

class Student:
    def __init__(self, name, student_id, score):
        self.name = name
        self.student_id = student_id
        self.score = score


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

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

    def find_student(self, student_id):
        for student in self.students:
            if student.student_id == student_id:
                return student
        return None

    def update_student_score(self, student_id, new_score):
        student = self.find_student(student_id)
        if student:
            student.score = new_score
            return True
        return False


# 建立學生管理物件
manager = StudentManager()

while True:
    print("請選擇功能:")
    print("1. 錄入學生成績")
    print("2. 查詢學生成績")
    print("3. 修改學生成績")
    print("0. 退出")
    choice = input("請輸入功能編號:")

    if choice == "1":
        # 錄入學生成績
        name = input("請輸入學生姓名:")  # 如輸入姓名:林淵達
        student_id = input("請輸入學生學號:")  # 如輸入學號:222190108
        score = float(input("請輸入學生成績:"))
        student = Student(name, student_id, score)
        manager.add_student(student)
        print("學生成績錄入成功")

    elif choice == "2":
        # 查詢學生成績
        student_id = input("請輸入學生學號:")  # 222190108
        student = manager.find_student(student_id)
        if student:
            print("學生姓名:", student.name)  # 林淵達
            print("學生成績:", student.score)
        else:
            print("未找到該學生")

    elif choice == "3":
        # 修改學生成績
        student_id = input("請輸入學生學號:")
        new_score = float(input("請輸入新的成績:"))
        if manager.update_student_score(student_id, new_score):
            print("成績修改成功")
        else:
            print("未找到該學生")

    elif choice == "0":
        # 退出程式
        break

    else:
        print("無效的功能編號,請重新輸入")

相關文章