选课系统

Posted fantsaymwq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了选课系统相关的知识,希望对你有一定的参考价值。

需求:选课系统开发,要求有四种角色:学校、学员、课程、讲师
详细要求:
1、创建北京、上海 2 所学校
2、创建linux , python , go 3个课程 , linuxpy 在北京开, go 在上海开
3、课程包含,周期,价格,通过学校创建课程
4、通过学校创建班级, 班级关联课程、讲师
5、创建学员时,选择学校,关联班级
6、创建讲师角色时要关联学校
7、提供两个角色接口
8、为学员、讲师、管理员分别提供用户界面,并提供对应功能:
a 学员视图, 可以注册, 交学费, 选择班级,
b 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
c 管理视图,创建讲师, 创建班级,创建课程
注1:上面的操作产生的数据都通过pickle序列化保存到文件里
注2:此作业必须画流程图,图中标注好不同类或对象之间的调用关系

技术分享图片

技术分享图片

binlogin

from conf.settings import LOGIN_PATH


def login(role):
    with open(LOGIN_PATH, "r", encoding="utf-8") as f:
        name = input("请输入用户名:")
        pwd = input("请输入密码:")
        for line in f:
            identify, username, password, area = line.strip().split(",")
            if identify == role:
                if name == username and password == pwd:
                    print("%s登陆成功!" % username)
                    return {"name": name, "identify": role, "area": area, "status": True}
        else:
            print("用户名或密码错误")
            return {"name": name, "identify": role, "area": area, "status": False}

binmain

import sys, pickle, os
from bin.login import login
from bin.register import register
from src.member import Manager, Student, Teacher, choice_area
from conf.settings import DB_PATH


def entrance():
    msg = ["学生", "教师", "管理员"]
    while True:
        for i, j in enumerate(msg, 1):
            print(i, j)
        num = input("请输入所选序号:")
        if num == "1":
            area = choice_area()
            return {"role": "Student", "area": area}
        elif num == "2":
            area = choice_area()
            return {"role": "Teacher", "area": area}
        elif num == "3":
            return {"role": "Manager", "area": "area"}
        else:
            print("输入错误,请重新输入")


def run():
    get = entrance()
    if get["role"] == "Student":
        msg = ["注册", "登陆"]
        for i, j in enumerate(msg, 1):
            print(i, j)
        num = input("请输入所选序号:")
        if num == "1":
            area = get["area"]
            result = register("Student", area)
            stu_path = os.path.join(DB_PATH, area)
            school_area = os.path.basename(stu_path)
            path = os.path.join(os.path.join(stu_path, "student"), result["name"])
            name_in = input("请输入姓名:").strip()
            age_in = input("请输入年龄:").strip()
            sex_in = input("请输入性别:").strip()
            phone_num_in = input("请输入电话:").strip()
            student = Student(name_in, area)
            student.msg["login_name"] = result["name"]
            student.msg["name"] = name_in
            student.msg["age"] = age_in
            student.msg["sex"] = sex_in
            student.msg["phone_num"] = phone_num_in
            student.msg["school_area"] = school_area
            with open(path, "wb") as f3:
                pickle.dump(student, f3)
                print("%s学生信息填写成功" % name_in)

        elif num == "2":
            result = login(get["role"])
        else:
            print("输入错误,请重新输入")
    else:
        result = login(get["role"])

    if result["status"]:
        if hasattr(sys.modules[__name__], result["identify"]):
            cls = getattr(sys.modules[__name__], result["identify"])
            obj = cls(result["name"], get["area"])
            while True:
                for i, j in enumerate(cls.opt_list, 1):
                    print(i, j[0])
                func = input("请输入所选序号:")
                if func.isdigit() and hasattr(obj, cls.opt_list[int(func) - 1][1]):
                    getattr(obj, cls.opt_list[int(func) - 1][1])()

bin egister

from conf.settings import LOGIN_PATH


def register(role, area):
    f = open(LOGIN_PATH, "r+", encoding="utf-8")
    login = {}
    flag2 = False
    for line in f:
        login_info = line.strip().split(",")
        login[login_info[1]] = login_info[0]
    while True:
        name = input("请输入登陆用户名:").strip()
        if name in login:
            if login[name] == role:
                print("该用户名已存在,请重新输入")
            else:
                flag2 = True
        else:
            flag2 = True
        while flag2:
            password = input("请输入密码:").strip()
            passwd = input("请再次输入密码:").strip()
            if passwd == password:
                print("%s注册成功" % name)
                msg = "%s,%s,%s,%s
" % (role, name, password, area)
                f.write(msg)
                return {"name": name, "identify": role, "area": area, "status": True}
            else:
                print("两次密码不同,请重新输入")

confsettings

import os

BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DB_PATH = os.path.join(BASE_PATH, "db")
LOGIN_PATH = os.path.join(DB_PATH, "login_info")
school_name = "luffycity"

srcmember

import os, pickle
from src.school import School, Course, Classes
from conf.settings import DB_PATH, school_name
from bin.register import register


def choice_area():
    """
    选择校区
    :return:
    """
    area_path = os.path.join(DB_PATH, "area")
    file_list = os.listdir(area_path)
    file_list.remove("__init__.py")
    for i, j in enumerate(file_list, 1):
        print(i, j)
    num = input("请输入校区序号:").strip()
    area = file_list[int(num) - 1]
    return area

class Common(object):
    def view_courses(self):
        """
        查看课程
        :return:
        """
        area = choice_area()
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), area)
        with open(sch_path, "rb") as sch:
            sch_data = pickle.load(sch)
        if len(sch_data.courses) == 0:
            print("目前没有课程")
        else:
            for i, j in enumerate(sch_data.courses, 1):
                print("序号:%s,课程名:%s,班级:%s"
                      % (i, j["course"], j["classes"]))
        return sch_data

    @staticmethod
    def view_courses_n(area):
        """
        查看课程(无选校区)
        :return:
        """
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), area)
        with open(sch_path, "rb") as sch:
            sch_data = pickle.load(sch)
        if len(sch_data.courses) == 0:
            print("目前没有课程")
        else:
            for i, j in enumerate(sch_data.courses, 1):
                print("序号:%s,课程名:%s,班级:%s"
                      % (i, j["course"], j["classes"]))
        return {"data": sch_data, "path": sch_data}

    def exit(self):
        """
        退出
        :return:
        """
        exit()


class Manager(Common):
    opt_list = [("创建校区", "create_school"),
                ("创建课程", "create_course"),
                ("创建班级", "create_class"),
                ("创建老师", "create_teacher"),
                ("查看课程", "view_courses"),
                ("查看老师", "view_teachers"),
                ("查看学生", "view_students"),
                ("查看选课情况", "view_select_details"),
                ("退出", "exit")]

    def __init__(self, name, area):
        self.name = name
        self.area = area


    def create_school(self):
        """
        创建校区
        :return:
        """
        name = school_name
        address = input("创建校区名称:").strip()
        school = School(name, address)
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), address)
        if os.path.isfile(sch_path):
            print("该校区已存在")
        else:
            with open(sch_path, "wb") as f:
                pickle.dump(school, f)
                print("%s校区创建成功" % address)

    def create_course(self):
        """
        创建课程
        :return:
        """
        area = choice_area()          # 选择校区
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), area)
        with open(sch_path, "rb") as sch:
            sch_data = pickle.load(sch)
        name = input("课程名称:").strip()
        price = input("学费为:").strip()
        period = input("课程周期为:").strip()
        course = Course(name, price, period)     # 实例化
        sch_cou = {"course": course.name, "price": course.price, "classes": "none", "students": [], "teachers": []}
        sch_data.courses.append(sch_cou)     # 课程名加入学校信息中
        with open(sch_path, "wb") as f:
            f.truncate(0)
            pickle.dump(sch_data, f)
        cou_path = os.path.join(os.path.join(os.path.join(DB_PATH, sch_data.addr), "course"), name)
        with open(cou_path, "wb") as cou:
            pickle.dump(course, cou)
        print("%s课程创建成功" % name)

    def create_class(self):
        """
        创建班级
        :return:
        """
        sch_data = self.view_courses()
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), sch_data.addr)
        area = sch_data.addr
        num = input("输入开班课程编号:").strip()
        sch_course = sch_data.courses[int(num) - 1]
        sch_course_name = sch_course["course"]
        sch_course_class = sch_course["classes"].split(",")
        name = input("班级名称:").strip()
        start_time = input("开班时间:").strip()
        classes = Classes(name, start_time)     # 实例化班级
        c_flag = False
        for i in sch_course_class:
            if i == "none":
                sch_course_class = []
                c_flag = True
            elif i == name:
                print("该班级已存在")
                return
            else:
                c_flag = True
        if c_flag:
            sch_course_class.append(name)
            sch_course["classes"] = ",".join(sch_course_class)   # 学校信息更改
            cou_path = os.path.join(os.path.join(os.path.join(DB_PATH, area), "course"), sch_course_name)
            with open(cou_path, "rb")as cou:
                cou_data = pickle.load(cou)
            cou_data.classes.append(classes)   # 课程信息更改
            with open(cou_path, "wb") as cou_w:
                cou_w.truncate(0)
                pickle.dump(cou_data, cou_w)
            with open(sch_path, "wb") as sch_w:
                sch_w.truncate(0)
                pickle.dump(sch_data, sch_w)
            print("%s校区%s课程%s班级创建成功" % (area, sch_course_name, name))

    def create_teacher(self):
        """
        创建老师
        :return:
        """
        area = choice_area()
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), area)
        with open(sch_path, "rb") as f:
            sch_data = pickle.load(f)
            result = register("Teacher", area)
            name_in = input("请输入教师姓名:").strip()
            age_in = input("请输入年龄:").strip()
            sex_in = input("请输入性别:").strip()
            salary_in = input("请输入工资:").strip()
            teacher = Teacher(name_in, area)
            teacher.msg["login_name"] = result["name"]
            teacher.msg["age"] = age_in
            teacher.msg["sex"] = sex_in
            teacher.msg["salary"] = salary_in
        teach_path = os.path.join(os.path.join(os.path.join(DB_PATH, area), "teacher"), result["name"])
        msg = {"login_name": result["name"], "name": name_in}
        sch_data.teachers.append(msg)
        with open(sch_path, "wb") as f:
            f.truncate(0)
            pickle.dump(sch_data, f)
        with open(teach_path, "wb") as f3:
            pickle.dump(teacher, f3)
            print("%s老师添加成功" % name_in)

    def view_teachers(self):
        """
        查看教师
        :return:
        """
        area = choice_area()
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), area)
        with open(sch_path, "rb") as f:
            sch_data = pickle.load(f)
            if len(sch_data.teachers) == 0:
                print("当前无老师")
                return
            else:
                while True:
                    print(‘33[35;1m %s 33[0m‘ % "老师信息".center(50, "-"))
                    for i, j in enumerate(sch_data.teachers, 1):
                        teach_name = j["name"]
                        print("序号:%s,姓名:%s,登录名称:%s" % (i, teach_name, j["login_name"]))
                    num = input("输入查看教师序号,退出请输入“0”:")
                    if num != "0" and num.isdigit():
                        login_name = sch_data.teachers[int(num)-1]["login_name"]
                        teach_path = os.path.join(os.path.join(os.path.join(DB_PATH, area), "teacher"), login_name)
                        with open(teach_path, "rb")as teach:
                            teach_data = pickle.load(teach)
                        course_list = []
                        if len(teach_data.course) == 0:
                            course_msg = "课程:"+ "none"
                        else:
                            for k in teach_data.course:
                                if len(k["class"]) == 0:
                                    msg = "课程:" + k["course"] + ",班级:" + "none"
                                else:
                                    for m in k["class"]:
                                        msg = "课程:" + k["course"] + ",班级:" + m
                                        course_list.append(msg)
                            course_msg = "
".join(course_list)
                        print(‘33[34;1m %s 33[0m‘ % "老师信息".center(50, "-"))
                        print("姓名:%s, 年龄:%s, 性别:%s, 工资:%s, 登录名:%s"
                              % (teach_name, teach_data.msg["age"], teach_data.msg["sex"],
                                 teach_data.msg["salary"], login_name))
                        print(course_msg)
                    else:
                        return

    def view_students(self):
        """
        查看学生
        :return:
        """
        area = choice_area()
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), area)
        with open(sch_path, "rb") as f:
            sch_data = pickle.load(f)
            while True:
                print(‘33[35;1m %s 33[0m‘ % "学生信息".center(50, "-"))
                for i, j in enumerate(sch_data.students, 1):
                    print("序号:%s,姓名:%s,登录名称:%s"
                          % (i, j["name"], j["login_name"]))
                num = input("选择查看学生的序号,结束请输“0”")
                if num != "0" and num.isdigit():
                    stu_msg = sch_data.students[int(num)-1]
                    stu_path = os.path.join(os.path.join(os.path.join(DB_PATH, area), "student"), stu_msg["login_name"])
                    with open(stu_path, "rb")as stu:
                        stu_data = pickle.load(stu)
                    course_list = []
                    if len(stu_data.course) == 0:
                        course_msg = "课程:none"
                    else:
                        for k in stu_data.course:
                            if len(k["class"]) == 0:
                                msg = "课程:" + k["course"] + ",班级:" + "none,成绩:" + k["score"]
                            else:
                                k["class"] = k["class"].split(",")
                                for m in k["class"]:
                                    msg = "课程:" + k["course"] + ",班级:" + m + ",成绩:" + k["score"]
                                    course_list.append(msg)
                        course_msg = "
".join(course_list)
                    print(‘33[34;1m %s 33[0m‘ % "学生信息".center(50, "-"))
                    print("姓名:%s,登录名称:%s, 性别:%s,年龄:%s,电话:%s"
                          % (stu_data.name, stu_data.msg["login_name"], stu_data.msg["sex"], stu_data.msg["age"],
                             stu_data.msg["phone_num"]))
                    print(course_msg)
                else:
                    return

    def view_select_details(self):
        """
        查看选课详情
        :return:
        """
        area = choice_area()
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), area)
        with open(sch_path, "rb") as f:  # 读取学校信息(pickle)
            sch_data = pickle.load(f)
            if len(sch_data.courses) == 0:
                print("当前没有课程")
            else:
                for i in sch_data.courses:
                    if len(i["students"]) == 0:
                        print("%s课程无人选课" % i["course"])
                    else:
                        print(‘33[34;1m %s 33[0m‘ % (i["course"]+"课程").center(20, "-"))
                        # print("%s课程" % i["course"])
                        for j in i["students"]:
                            print("学生:%s" % j["name"])


# 注册、缴费、查看可选课程、选择课程、查看所选课程、查看成绩
class Student(Common):
    opt_list = [("查看课程列表", "view_courses"),
                ("选择课程", "choice_grade"),
                ("缴费", "pay"),
                ("选择班级", "choice_classes"),
                ("查看所选课程", "view_select"),
                ("查看成绩", "view_score"),
                ("退出", "exit")]

    def __init__(self, name, area):
        self.name = name
        self.area = area
        self.msg = {"login_name": "name", "name": "name", "age": "age", "sex": "sex", "phone_num": "num"}
        self.course = []
        self.unpaid = []

    def choice_grade(self):
        """
        选择课程
        :return:
        """
        stu_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "student"), self.name)
        with open(stu_path, "rb") as stu:  # 读取学生文件信息(pickle)
            stu_data = pickle.load(stu)
        school = self.view_courses_n(self.area)
        sch_data = school["data"]
        num = input("输入选课序号,添加结束请输“0”").strip()
        if num != "0" and num.isdigit():
            sch_curriculum = sch_data.courses[int(num) - 1]
            c_flag1 = c_flag2 = False
            if len(sch_curriculum["students"]) != 0:
                for i in sch_curriculum["students"]:
                    if i == self.name:
                        print("该课程已选购")
                        return
                    else:
                        c_flag1 = True
            else:
                c_flag1 = True
            if c_flag1:
                if len(stu_data.unpaid) != 0:
                    for n in stu_data.unpaid:
                        if n["name"] == sch_curriculum["course"]:
                            print("该课程已选择")
                            return
                        else:
                            c_flag2 = True
                else:
                    c_flag2 = True
                if c_flag2:
                    cou = {"name": sch_curriculum["course"], "price": sch_curriculum["price"]}
                    stu_data.unpaid.append(cou)
                    with open(stu_path, "wb") as stu_w:     # 将学生信息写入文件(pickle)
                        stu_w.truncate(0)
                        pickle.dump(stu_data, stu_w)
                        print("%s选课成功,请前往缴费" % sch_curriculum["course"])
        else:
            return

    def pay(self):
        """
        缴费
        :return:
        """
        stu_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "student"), self.name)
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), self.area)
        with open(stu_path, "rb") as stu:
            stu_data = pickle.load(stu)
            if len(stu_data.unpaid) != 0:
                for i, j in enumerate(stu_data.unpaid, 1):
                    print(i, j["name"])
                cou_num = input("请选择缴费课程:")
                if cou_num.isdigit():
                    if int(cou_num) <= len(stu_data.unpaid)+1:
                        print("%s课程需缴费%s元" % (stu_data.unpaid[int(cou_num)-1]["name"],
                                              stu_data.unpaid[int(cou_num)-1][‘price‘]))
                        money = int(input("请输入转账金额:"))
                        if money >= int(stu_data.unpaid[int(cou_num)-1][‘price‘]):
                            cou_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area),
                                                                 "course"), stu_data.unpaid[int(cou_num) - 1]["name"])
                            course_name = stu_data.unpaid[int(cou_num)-1]["name"]
                            stu_data.course.append({"course": course_name, "class": "none", "score": "none"})
                            stu_data.unpaid.pop(int(cou_num) - 1)   # 学生中加入缴费后的课程

                            with open(cou_path, "rb") as cou_r:
                                cou_data = pickle.load(cou_r)                           # 课程中添加学生信息
                            cou_data.students.append({"name": stu_data.name, "login_name": self.name})
                            with open(cou_path, "wb") as cou_w:
                                cou_w.truncate(0)
                                pickle.dump(cou_data, cou_w)
                            stu_w = open(stu_path, "wb")
                            stu_w.truncate(0)
                            pickle.dump(stu_data, stu_w)
                            with open(sch_path, "rb") as f:  # school
                                sch_data = pickle.load(f)
                            flag = False
                            if len(sch_data.students) == 0:
                                flag = True
                            else:
                                for x in sch_data.students:
                                    if x["login_name"] == self.name:
                                        pass
                                    else:
                                        flag =True
                            if flag:                # 学校加入学生
                                sch_data.students.append({"name": stu_data.name, "login_name": self.name})
                            for n in sch_data.courses:
                                if n["course"] == course_name:
                                    n["students"].append({"name": stu_data.name, "login_name": self.name})
                            with open(sch_path, "wb") as f:
                                f.truncate(0)
                                pickle.dump(sch_data, f)
                            print("%s课程缴费完成,请前往选择班级" % course_name)
                            return
                        else:
                            print("金额不足")
                else:
                    print("输入错误")
            else:
                print("没有需缴费课程")

    def choice_classes(self):    # 选择课程重复
        """
        选择班级
        :return:
        """
        stu_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "student"), self.name)
        # sch_path = os.path.join(os.path.join(DB_PATH, "area"), self.area)
        with open(stu_path, "rb") as stu:
            stu_data = pickle.load(stu)
        if len(stu_data.course) == 0:
            print("当前没有课程")
            return
        else:
            for i, j in enumerate(stu_data.course, 1):
                print("序号:%s,课程:%s" % (i, j["course"]))
            stu_cou_num = input("输入选择课程的序号:")
            if stu_cou_num.isdigit() and int(stu_cou_num) <= len(stu_data.course)+1:
                stu_cou = stu_data.course[int(stu_cou_num)-1]
                stu_cou_name = stu_cou["course"]
                if stu_cou["class"] != "none":
                    print("该课程已选择班级")
                    return
                else:
                    cou_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "course"), stu_cou_name)
                    with open(cou_path, "rb") as cou:
                        cou_data = pickle.load(cou)
                    if len(cou_data.classes) == 0:
                        print(‘当前课程没有开设班级‘)
                        return
                    else:
                        for m, n in enumerate(cou_data.classes, 1):
                            print("序号:%s,班级:%s" % (m, n.name))
                        cla_num = input("输入选择班级的序号:")
                        if cla_num.isdigit() and int(cla_num) <= len(cla_num) + 1:
                            cla_data = cou_data.classes[int(cla_num)-1]   # 课程中添加学生
                            cla_data.students.append({"name": stu_data.name, "login_name": self.name, "score": ""})
                            stu_cou["class"] = cla_data.name    # 学生信息中加入班级
                            with open(cou_path, "wb") as cou_w:
                                cou_w.truncate(0)
                                pickle.dump(cou_data, cou_w)
                            with open(stu_path, "wb") as stu_w:
                                stu_w.truncate(0)
                                pickle.dump(stu_data, stu_w)
                            print("班级选择成功")
                        else:
                            print("输入错误")
            else:
                print("输入错误")

    def view_select(self):
        """
        查看所选课程
        :return:
        """
        stu_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "student"), self.name)
        with open(stu_path, "rb") as stu:
            stu_data = pickle.load(stu)
            if len(stu_data.course) != 0:
                print("所选课程为:")
                for i, j in enumerate(stu_data.course, 1):
                    print("序号:%s,课程名:%s,班级:%s" % (i, j["course"], j["class"]))
            else:
                print("当前无课程")

    def view_score(self):
        """
        查看成绩
        :return:
        """
        stu_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "student"), self.name)
        with open(stu_path, "rb") as stu:
            stu_data = pickle.load(stu)
            if len(stu_data.course) != 0:
                for i, j in enumerate(stu_data.course, 1):
                    print("序号:%s,课程名:%s,成绩:%s" % (i, j["course"], j["score"]))
            else:
                print("当前无课程")


# 选择班级,查看学生列表,修改成绩
class Teacher(Common):
    opt_list = [("选择课程", "choice_grade"),
                ("选择班级", "choice_classes"),
                ("查看学生详情", "view_student"),
                ("修改成绩", "change_score"),
                ("退出", "exit")]

    def __init__(self, name, area):
        self.name = name
        self.area = area
        self.msg = {"login_name": "name", "age": 0, "sex": "male", "salary": "10"}
        self.course = []

    def choice_grade(self):
        """
        选择课程
        :return:
        """
        sch_path = os.path.join(os.path.join(DB_PATH, "area"), self.area)
        teach_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "teacher"), self.name)
        with open(teach_path, "rb") as teach:        # 读取老师文件信息(pickle)
            teach_data = pickle.load(teach)
        school = self.view_courses_n(self.area)
        sch_data = school["data"]                    # 读取学校文件信息(pickle)
        num = input("输入选课序号,添加结束请输“0”").strip()
        if num != "0" and num.isdigit():
            sch_curriculum = sch_data.courses[int(num) - 1]
            course_name = sch_curriculum["course"]
            c_flag1 = False
            if len(teach_data.course) != 0:
                for i in teach_data.course:
                    if i["course"] == course_name:
                        print("该课程已添加")
                        return
                    else:
                        c_flag1 = True
            else:
                c_flag1 = True
            if c_flag1:
                cou_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "course"), course_name)
                teach_data.course.append({"course": course_name, "class": []})     # 老师信息中加入课程
                with open(cou_path, "rb") as cou_r:
                    cou_data = pickle.load(cou_r)
                cou_data.teachers.append({"name": teach_data.name, "login_name": self.name})  # 课程中添加老师信息
                with open(cou_path, "wb") as cou_w:
                    cou_w.truncate(0)
                    pickle.dump(cou_data, cou_w)
                stu_w = open(teach_path, "wb")
                stu_w.truncate(0)
                pickle.dump(teach_data, stu_w)
                flag = False
                for x in sch_data.teachers:
                    if x["login_name"] == self.name:
                        pass
                    else:
                        flag = True
                if flag:
                    sch_data.teachers.append({"name": teach_data.name, "login_name": self.name})   # 学校中加入老师
                for n in sch_data.courses:
                    if n["course"] == course_name:
                        n["teachers"].append({"name": teach_data.name, "login_name": self.name})  # 学校课程中加入老师
                with open(sch_path, "wb") as f:
                    f.truncate(0)
                    pickle.dump(sch_data, f)
                print("%s课程添加完成,请前往选择班级" % course_name)
                return
        else:
            return

    def choice_classes(self):
        """
        选择班级
        :return:
        """
        teach_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "teacher"), self.name)
        with open(teach_path, "rb") as teach:
            teach_data = pickle.load(teach)
        if len(teach_data.course) == 0:
            print("当前没有课程")
            return
        else:
            for i, j in enumerate(teach_data.course, 1):
                print("序号:%s,课程:%s" % (i, j["course"]))
            teach_cou_num = input("输入选择课程的序号:")
            if teach_cou_num.isdigit() and int(teach_cou_num) <= len(teach_data.course)+1:
                teach_cou = teach_data.course[int(teach_cou_num)-1]
                cou_name = teach_cou["course"]
                cou_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "course"), cou_name)
                with open(cou_path, "rb") as cou:
                    cou_data = pickle.load(cou)
                if len(cou_data.classes) == 0:
                    print(‘当前课程没有开设班级‘)
                    return
                else:
                    for m, n in enumerate(cou_data.classes, 1):
                        print("序号:%s,班级:%s" % (m, n.name))
                    cla_num = input("输入选择班级的序号:")
                    c_flag = False
                    if cla_num.isdigit() and int(cla_num) <= len(cla_num) + 1:
                        cla_data = cou_data.classes[int(cla_num)-1]
                        if len(cla_data.teachers) != 0:
                            for i in cla_data.teachers:
                                if i["login_name"] == self.name:
                                    print("该班级已添加!")
                                    return
                                else:
                                    c_flag = True
                        else:
                            c_flag = True
                if c_flag:
                    cla_data.teachers.append({"name": teach_data.name, "login_name": self.name})  # 课程班级中添加老师
                    teach_cou["class"].append(cla_data.name)    # 老师信息中加入班级
                    with open(cou_path, "wb") as cou_w:
                        cou_w.truncate(0)
                        pickle.dump(cou_data, cou_w)
                    with open(teach_path, "wb") as teach_w:
                        teach_w.truncate(0)
                        pickle.dump(teach_data, teach_w)
                    print("班级选择成功")
            else:
                print("输入错误")

    def view_student(self):
        """
        查看学生详情
        :return:
        """
        teach_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "teacher"), self.name)
        with open(teach_path, "rb") as teach:
            teach_data = pickle.load(teach)
        if len(teach_data.course) == 0:
            print("教师当前未选课程")
        else:
            for i, j in enumerate(teach_data.course, 1):
                print("序号:%s,课程:%s" % (i, j["course"]))
            cou_num = input("选择课程的序号:")
            # print(teach_data.course)
            if cou_num.isdigit() and int(cou_num) <= len(teach_data.course) + 1:
                t_cou_msg = teach_data.course[int(cou_num) - 1]
                if len(t_cou_msg["class"]) == 0:
                    print("该课程未选择班级")
                    return
                else:
                    # print(t_cou_msg["class"])
                    for q in t_cou_msg["class"]:
                        print("序号:%s,班级:%s" % (t_cou_msg["class"].index(q)+1, q))
                    cla_num = input("选择班级的序号:")
                    if cla_num.isdigit() and int(cla_num) <= len(t_cou_msg["class"]) + 1:
                        cla_name = t_cou_msg["class"][int(cla_num)-1]
                        cou_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area),
                                                             "course"), t_cou_msg["course"])
                        with open(cou_path, "rb")as cou:
                            cou_data = pickle.load(cou)
                        for m in cou_data.classes:
                            if m.name == cla_name:
                                cla_data = cou_data.classes[int(cla_num) - 1]
                                if len(cla_data.students) == 0:
                                    print("该班级当前没有学生")
                                    break
                                else:
                                    for a, b in enumerate(cla_data.students, 1):
                                        print("序号:%s,学生:%s" % (a, b["name"]))
                                    stu_num = input("选择学生的序号:")
                                    if stu_num.isdigit() and int(stu_num) <= len(cla_data.students) + 1:
                                        stu_msg = cla_data.students[int(stu_num) - 1]
                                        stu_lname = stu_msg["login_name"]
                                        stu_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area),
                                                                             "student"), stu_lname)
                                        with open(stu_path, "rb") as stu:
                                            stu_data = pickle.load(stu)
                                        cou_list = []
                                        for k in cla_data.students:
                                            if k["login_name"] == stu_lname:
                                                score = k["score"]
                                                msg = "课程:" + t_cou_msg[
                                                    "course"] + ",班级:" + cla_data.name + ",成绩:" + score
                                            else:
                                                msg = "课程:" + t_cou_msg[
                                                    "course"] + ",班级:" + cla_data.name + ",成绩:none"
                                            cou_list.append(msg)
                                        course_msg = "
".join(cou_list)
                                        print("姓名:%s,登录名称:%s, 性别:%s,年龄:%s,电话:%s"
                                              % (stu_data.name, stu_data.msg["login_name"], stu_data.msg["sex"],
                                                 stu_data.msg["age"], stu_data.msg["phone_num"]))
                                        print(course_msg)
                                        msg = {"student": stu_data.msg["login_name"],
                                               "course": t_cou_msg["course"], "class": cla_data.name}
                                        return msg
                            else:
                                print("该班级当前没有学生")
                    else:
                        print("输入错误")
            else:
                print("输入错误")

    def change_score(self):
        """
        更改学生成绩
        :return:
        """
        try:
            msg = self.view_student()
            stu_name = msg["student"]
            cou_name = msg["course"]
            cla_name = msg["class"]
            cou_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "course"), cou_name)
            stu_path = os.path.join(os.path.join(os.path.join(DB_PATH, self.area), "student"),  stu_name)
            with open(stu_path, "rb") as stu:
                stu_data = pickle.load(stu)
            with open(cou_path, "rb") as cou:
                cou_data = pickle.load(cou)
            score = input("请输入%s的成绩:" % cou_name)
            if score.isdigit() and 0 <= int(score) <= 100:
                for i in stu_data.course:
                    if i["course"] == cou_name:
                        i["score"] = score
                for j in cou_data.classes:
                    if j.name == cla_name:
                        for m in j.students:
                            if m["login_name"] == stu_name:
                                m["score"] = score
                with open(stu_path, "wb") as stu_w:
                    stu_w.truncate(0)
                    pickle.dump(stu_data, stu_w)
                with open(cou_path, "wb") as cou_w:
                    cou_w.truncate(0)
                    pickle.dump(cou_data, cou_w)
                print("%s学生%s课程成绩修改成功" % (stu_name, cou_name))
                return
            else:
                print("成绩输入错误")
        except TypeError:
            print("成绩修改失败")

srcschool

class School:
    def __init__(self, name, addr):
        self.name = name
        self.addr = addr
        self.courses = []
        self.teachers = []
        self.students = []


class Course(object):
    def __init__(self, name, price, period):
        self.name = name
        self.price = price
        self.period = period
        self.classes = []
        self.teachers = []
        self.students = []


class Classes(object):
    def __init__(self, name, start_time):
        self.name = name
        self.start_time = start_time
        self.teachers = []
        self.students = []

初始化

import os
import pickle
from conf.settings import DB_PATH, LOGIN_PATH
from src.school import School, Course

msg = "Manager,admin,123,area
"
with open(LOGIN_PATH, "w", encoding="utf-8") as f:
    f.truncate(0)
    f.write(msg)

name = "luffycity"
address = "beijing"
address2 = "shanghai"
AREA_PATH = os.path.join(DB_PATH, "area")
area_path = os.path.join(AREA_PATH, address)
area_path2 = os.path.join(AREA_PATH, address2)
school = School(name, address)
school2 = School(name, address2)
name1 = "python"
price1 = "10000"
period1 = "6mons"
name2 = "linux"
price2 = "20000"
period2 = "7mons"
name = "go"
price = "30000"
period= "8mons"
course = Course(name, price, period)     # 实例化
course1 = Course(name1, price1, period1)
course2 = Course(name2, price2, period2)
sch_cou1 = {"course": course1.name, "price": course1.price, "classes": "none", "students": [], "teachers": []}
sch_cou2 = {"course": course2.name, "price": course2.price, "classes": "none", "students": [], "teachers": []}
sch_cou = {"course": course.name, "price": course.price, "classes": "none", "students": [], "teachers": []}
school.courses.append(sch_cou1)     # 课程名加入学校信息中
school.courses.append(sch_cou2)     # 课程名加入学校信息中
school2.courses.append(sch_cou)     # 课程名加入学校信息中
with open(area_path, "wb") as f:
    pickle.dump(school, f)
    f.close()
with open(area_path2, "wb") as f1:
    pickle.dump(school2, f1)
    f1.close()
path1 = os.path.join(os.path.join(os.path.join(os.path.join(DB_PATH, address)), "course"), name1)
path2 = os.path.join(os.path.join(os.path.join(os.path.join(DB_PATH, address)), "course"), name2)
path = os.path.join(os.path.join(os.path.join(os.path.join(DB_PATH, address2)), "course"), name)

with open(path1, "wb") as f1:
    pickle.dump(course1, f1)
with open(path2, "wb") as f2:
    pickle.dump(course2, f2)
with open(path, "wb") as f:
    pickle.dump(course, f)

 start.py

from bin.main import run

if __name__ == "__main__":
    run()

README

数据初始化在“初始化.py”中(两个校区,北京:linux、python,上海:go)
程序开始,运行start.py
初始带有一个管理员(账号:admin,密码:123)
数据信息都在db中,登录信息存在login_info,其余信息通过pickle保存在文件中
老师的登陆信息及信息详情由管理员创建

  

 

 















以上是关于选课系统的主要内容,如果未能解决你的问题,请参考以下文章

JAVA | 学生选课系统

自制简单选课系统模拟代码

XK关系型学生选课数据库,关系如下:

面向对象的应用:学生选课系统

数据库实验(学生选课系统)

选课系统作业