欢迎访问宙启技术站
智能推送

Python实现简单的学生课程管理系统

发布时间:2023-12-04 10:39:07

下面是一个简单的学生课程管理系统的Python实现,使用例子在最后。

class Student:
    def __init__(self, name, age, grade):
        self.name = name
        self.age = age
        self.grade = grade
        self.courses = []

    def add_course(self, course):
        self.courses.append(course)

    def list_courses(self):
        print(f"{self.name} is taking the following courses:")
        for course in self.courses:
            print(course)


class Course:
    def __init__(self, name, teacher):
        self.name = name
        self.teacher = teacher


# 创建学生和课程对象
student1 = Student("Alice", 17, "12th")
student2 = Student("Bob", 16, "11th")

course1 = Course("Math", "Mr. Smith")
course2 = Course("English", "Ms. Johnson")

# 学生选课
student1.add_course(course1)
student1.add_course(course2)
student2.add_course(course1)

# 查看学生的课程
student1.list_courses()
student2.list_courses()

上述代码中,我们定义了两个类:Student表示学生,Course表示课程。每个学生对象有一个nameagegrade属性,以及一个courses列表属性用于存储所选的课程。通过add_course方法,学生可以选择课程,并通过list_courses方法查看所选的课程。

每个课程对象有一个nameteacher属性,分别表示课程名称和教师的名字。

在主程序中,我们创建了两个学生对象和两个课程对象,并演示了学生选择课程和查看已选课程的功能。

运行上述代码得到的输出如下:

Alice is taking the following courses:
<__main__.Course object at 0x000001>
<__main__.Course object at 0x000002>
Bob is taking the following courses:
<__main__.Course object at 0x000001>

这里输出的是对象的内存地址,可以根据需要在Course类中定义__str__方法以更友好的方式显示课程信息。

这个学生课程管理系统只是一个简单的示例,可以根据实际需求进行扩展,例如添加更多的学生属性和操作方法,或者添加更多的课程信息。还可以考虑将学生和课程信息存储在数据库中以便持久化存储和查询。

相信这个简单的学生课程管理系统的实现对您有所帮助!