You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python学生选课系统问题:课程未与对应学生绑定求助

学生选课系统课程绑定问题解决方案

核心问题

当前所有课程都存入全局courseList,未与具体学生关联,导致查询时显示所有学生的课程。

修改方案

  1. 给Student类添加专属课程列表,每个学生的课程独立存储
  2. 修正代码中构造函数拼写、变量命名不统一、函数调用错误等问题
  3. 调整课程添加和查询逻辑,直接操作对应学生的课程列表

修正后的完整代码

# 初始化学生列表
student_list = []

class Student:
    def __init__(self, student_code, name):
        self.student_code = student_code
        self.name = name
        self.courses = []  # 每个学生专属的课程列表

class Course:
    def __init__(self, course_name):
        self.course_name = course_name

def menu():
    select = 0
    while select != 5:
        print("Welcome students")
        print("choose one of the options")
        print("----------------------------")
        print("1. Add new students")
        print("2. Show registered students")
        print("3. Add course")
        print("4. Student's registered courses")
        print("5. Quit")
        select = int(input("Choose an option: "))

        if select == 1:
            add_student()
        elif select == 2:
            show_students()
        elif select == 3:
            add_course()
        elif select == 4:
            show_student_courses()
        elif select == 5:
            exit_program()

def add_student():
    print("")
    student_code = input("Enter the student code: ")
    name = input("Enter name: ")
    student = Student(student_code, name)
    student_list.append(student)
    print(f"Student {name} registered successfully")

def show_students():
    print(f"\nThere are {len(student_list)} students records")
    for idx, student in enumerate(student_list, 1):
        print(f"#{idx}")
        print(f"The student's name is: {student.name}")
        print(f"The student code is: {student.student_code}")
        print("---")

def add_course():
    print("")
    target_code = input("Enter the student code: ")
    print("Looking for student registration...")
    found = False
    for student in student_list:
        if student.student_code == target_code:
            found = True
            course_name = input("Enter the course name: ")
            course = Course(course_name)
            student.courses.append(course)
            print(f"Course {course_name} added to {student.name}'s list")
            break
    if not found:
        print("Student not found")

def show_student_courses():
    target_code = input("\nEnter the student code to see their registered courses: ")
    found = False
    for student in student_list:
        if student.student_code == target_code:
            found = True
            print(f"\n{student.name}'s registered courses:")
            if not student.courses:
                print("No courses registered yet")
            else:
                for idx, course in enumerate(student.courses, 1):
                    print(f"{idx}. {course.course_name}")
            break
    if not found:
        print("Student not found")

def exit_program():
    print("See you later")

# 启动菜单
menu()

关键修改说明

  • Student类新增courses属性:每个实例自带一个空列表,专门存储该学生的课程
  • 移除全局courseList:不再使用全局列表存储所有课程,避免混淆
  • 统一命名规范:将studentCod/codStudent统一为student_code,courseName改为course_name,提升代码可读性
  • 修正构造函数:把错误的__int__改为Python类正确的初始化方法__init__
  • 优化课程添加逻辑:找到对应学生后,直接将课程添加到其courses列表,并增加学生不存在的提示
  • 优化查询逻辑:仅遍历目标学生的courses列表,确保只显示该学生的课程

内容的提问来源于stack exchange,提问作者Sidny paredes garcia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 06:55:22