选课系统

Posted zhongguiyao

tags:

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

 

管理员:
    创建老师:姓名、性别、年龄、资产
    创建课程:课程名称、上课时间、课时费、关联老师
    使用pickle保存在文件
   
学生:
    学生:用户名、密码、性别、年龄、选课列表、上课记录
    1.列举所有课程
    2.选择课程
    3.学生上课
    4.ret = 课程.work() 获取课程的返回值;资产+课时费
   
注意:在python2版本,如果文件夹中没有__init__文件,是无法导入包的
   

作业讲解之管理员登录

step1:新建day13_homework文件夹,在该文件夹下新建administrator.py文件和lib包,lib包下新建models.py文件

step2:修改并执行administrator.py文件

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,xb))


def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    if inp == "1":
        pass
    elif inp == "2":
        user = input("请输入用户名:")
        pwd = input("请输入密码:")
        admin_obj = Admin()
        admin_obj.register(user,pwd)

if __name__ == __main__:
    main()

执行结果:

1、管理员登录;2、管理员注册;
 >>>2
请输入用户名:alex
请输入密码:123


1、管理员登录;2、管理员注册;
 >>>2
请输入用户名:serven
请输入密码:123

 

step3:修改并执行administrator.py

 

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,xb))


def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    if inp == "1":
        user = input("请输入用户名:")
        pwd = input("请输入密码:")
        import os
        if os.path.exists(user):
            admin_obj = pickle.load(open(user,rb))
            if admin_obj.login(user,pwd):
                print("登录成功")
            else:
                print("登录失败")
        else:
            print("用户不存在")
        
    elif inp == "2":
        user = input("请输入用户名:")
        pwd = input("请输入密码:")
        admin_obj = Admin()
        admin_obj.register(user,pwd)

if __name__ == __main__:
    main()
    

 

###############################################

作业讲解之创建老师

 

step1:修改并执行administrator.py

 

 

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,xb))

def create_course(admin_obj):
    teacher_list = pickle.load(open("teacher_list",rb))
    for item in teacher_list:
        print(item.name,item.age,item.create_time,item.create_admin.username)

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists("teacher_list"):
        exists_list = pickle.load(open("teacher_list",rb))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open("teacher_list",wb))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,rb))
        if admin_obj.login(user,pwd):
                print("登录成功")
                sel = input("1、创建老师;2、创建课程")
                if sel == "1":
                    create_teacher(admin_obj)
                elif sel == "2":
                    create_course(admin_obj)
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()
    

执行结果:

D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/index.py
1、管理员登录;2、管理员注册;
 >>>1
请输入用户名:alex
请输入密码:123
登录成功
1、创建老师;2、创建课程1
请输入老师姓名(q表示退出) xiaojun
请输入老师年龄: 99
请输入老师姓名(q表示退出) wangheng
请输入老师年龄: 98
请输入老师姓名(q表示退出) suoning
请输入老师年龄: 9
请输入老师姓名(q表示退出) yangling
请输入老师年龄: 73
请输入老师姓名(q表示退出) q

Process finished with exit code 0

 

step2:修改并执行administrator.py

 

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,xb))

def create_course(admin_obj):
    teacher_list = pickle.load(open("teacher_list",rb))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)

    input("请输入课程名称....: ")
    input("请选择老师 1,2,3,4...:")


def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists("teacher_list"):
        exists_list = pickle.load(open("teacher_list",rb))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open("teacher_list",wb))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,rb))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()
    
    

 

step3:修改并执行administrator.py

 

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Course:
    """
    课程相关信息
    """
    def __init__(self,course_name,cost,teacher_obj,admin):
        self.course_name = course_name
        self.cost = cost
        self.teacher = teacher_obj
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,xb))

def create_course(admin_obj):
    teacher_list = pickle.load(open("teacher_list",rb))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists("course_list"):
        exists_list = pickle.load(open("course_list",rb))
        course_list.extend(exists_list)

    pickle.dump(course_list,open("course_list",wb))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists("teacher_list"):
        exists_list = pickle.load(open("teacher_list",rb))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open("teacher_list",wb))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,rb))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()

 

 

执行结果:

D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/index.py
1、管理员登录;2、管理员注册;
 >>>1
请输入用户名:alex
请输入密码:123
登录成功
1、创建老师;2、创建课程2
1 xiaojun 99 2017-07-12 21:24:59 alex
2 wangheng 98 2017-07-12 21:25:09 alex
3 suoning 9 2017-07-12 21:25:21 alex
4 yangling 73 2017-07-12 21:25:34 alex
请输入课程名称(q表示退出): 生物
请选择课时费:10
请选择老师(序号):1
请输入课程名称(q表示退出): 数学
请选择课时费:4
请选择老师(序号):4
请输入课程名称(q表示退出): 体育
请选择课时费:3
请选择老师(序号):3
请输入课程名称(q表示退出): q
1、创建老师;2、创建课程q

Process finished with exit code 0

 

###########################################


作业讲解之程序目录

 

step1:修改lib包下models.py

 

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Course:
    """
    课程相关信息
    """
    def __init__(self,course_name,cost,teacher_obj,admin):
        self.course_name = course_name
        self.cost = cost
        self.teacher = teacher_obj
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,xb))
        

 

step2:修改administrator.py

 

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os
from lib import models

def create_course(admin_obj):
    teacher_list = pickle.load(open("teacher_list",rb))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = models.Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists("course_list"):
        exists_list = pickle.load(open("course_list",rb))
        course_list.extend(exists_list)

    pickle.dump(course_list,open("course_list",wb))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = models.Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists("teacher_list"):
        exists_list = pickle.load(open("teacher_list",rb))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open("teacher_list",wb))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,rb))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = models.Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()
    

step3:在day13_homework文件夹下新建db文件夹,把之前生成的alex、course_list、seven和teacher_list文件放到db文件夹中

step4:在day13_homework文件夹下新建config文件夹,在config文件夹中新建settings.py文件:

step5:修改settings.py文件:

 

#!/usr/bin/env python
#coding:utf8

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

BASE_ADMIN_DIR = os.path.join(BASE_DIR,"db","admin")

TEACHER_DB_DIR = os.path.join(BASE_DIR,"db","teacher_list")

COURSE_DB_DIR = os.path.join(BASE_DIR,"db","course_list")

 

step6:在db文件夹下新建admin文件夹,把alex和seven文件放到该文件夹:

 

step7:修改lib下的models.py

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os
from config import settings


class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Course:
    """
    课程相关信息
    """
    def __init__(self,course_name,cost,teacher_obj,admin):
        self.course_name = course_name
        self.cost = cost
        self.teacher = teacher_obj
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd
        path = os.path.join(settings.BASE_ADMIN_DIR,self.username)
        pickle.dump(self,open(path,xb))

 

step8:执行administrator.py文件

 

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os
from lib import models
from config import settings

def create_course(admin_obj):
    teacher_list = pickle.load(open(settings.TEACHER_DB_DIR,rb))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = models.Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists(settings.COURSE_DB_DIR):
        exists_list = pickle.load(open(settings.COURSE_DB_DIR,rb))
        course_list.extend(exists_list)

    pickle.dump(course_list,open(settings.COURSE_DB_DIR,wb))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = models.Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists(settings.TEACHER_DB_DIR):
        exists_list = pickle.load(open(settings.TEACHER_DB_DIR,rb))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open(settings.TEACHER_DB_DIR,wb))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,rb))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = models.Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()
    

 

 

   
执行结果:

 

1、管理员登录;2、管理员注册;
 >>>2
请输入用户名:ahu
请输入密码:123

 

step9:在day13_homework文件夹下新建bin和log文件夹,把administrator.py和students.py放到bin文件夹下:

 

step10:修改并执行administrator.py

 

#!/usr/bin/env python
#coding:utf8

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import random
import time
import pickle

from lib import models
from config import settings


def create_course(admin_obj):
    teacher_list = pickle.load(open(settings.TEACHER_DB_DIR,rb))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = models.Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists(settings.COURSE_DB_DIR):
        exists_list = pickle.load(open(settings.COURSE_DB_DIR,rb))
        course_list.extend(exists_list)

    pickle.dump(course_list,open(settings.COURSE_DB_DIR,wb))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = models.Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists(settings.TEACHER_DB_DIR):
        exists_list = pickle.load(open(settings.TEACHER_DB_DIR,rb))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open(settings.TEACHER_DB_DIR,wb))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,rb))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = models.Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()
    

 

   
执行结果:
1、管理员登录;2、管理员注册;

 

###############################

作业讲解之选课

step1:在db文件夹下新建students文件夹

step2:修改config文件夹下的settings.py文件:

#!/usr/bin/env python
#coding:utf8

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

BASE_ADMIN_DIR = os.path.join(BASE_DIR,"db","admin")

BASE_STUDENTS_DIR = os.path.join(BASE_DIR,"db","students")

TEACHER_DB_DIR = os.path.join(BASE_DIR,"db","teacher_list")

COURSE_DB_DIR = os.path.join(BASE_DIR,"db","course_list")

 

step3:修改lib包下的models.py文件:

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os
from config import settings


class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Course:
    """
    课程相关信息
    """
    def __init__(self,course_name,cost,teacher_obj,admin):
        self.course_name = course_name
        self.cost = cost
        self.teacher = teacher_obj
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd
        path = os.path.join(settings.BASE_ADMIN_DIR,self.username)
        pickle.dump(self,open(path,xb))

class Student:
    """
    学生相关信息
    """
    def __init__(self):
        self.username = None
        self.password = None

        self.course_list = []
        self.study_dict = {}

    def select_course(self,course_obj):
        """
        学生选课
        :param course_obj:
        :return:
        """
        self.course_obj.append(course_obj)

    def study(self,course_obj):
        """
        学生上课
        :param course_obj:
        :return:
        """
        class_result = course_obj.have_lesson()

        if course_obj in self.study_dict.keys():
            self.study_dict[course_obj].append(class_result)
        else:
                self.study_dict[course_obj] = [class_result,]

    def login(self,user,pwd):
        """
        学生登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False

    def register(self,user,pwd):
        """
        学生注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = os.path.join(settings.BASE_STUDENTS_DIR,self.username)
        pickle.dump(self,open(path,"xb"))

 

step4:修改并执行bin文件夹下的students.py

#!/usr/bin/env python
#coding:utf8
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from lib import models
from config import settings


def register(user,pwd):
    obj = models.Student()
    obj.register(user,pwd)

def main():
    inp =input("1、登录;2、注册")
    user  = input("用户名: ")
    pwd = input("密码: ")

    if inp == "1":
        pass
    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()

 

执行结果:

1、登录;2、注册2
用户名: xiaoming
密码: 123


1、登录;2、注册2
用户名: xiaoli
密码: 123

 

step5:修改并执行bin文件夹下的students.py

#!/usr/bin/env python
#coding:utf8
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from lib import models
from config import settings
import pickle
from lib.models import Course
from lib.models import Admin
from lib.models import Teacher

def select_course(student_obj):
    course_list = pickle.load(open(settings.COURSE_DB_DIR,rb))
    for item in course_list:
        print(item.course_name,item.cost,item.teacher.name)

def login(user,pwd):
    if os.path.exists(os.path.join(settings.BASE_STUDENTS_DIR,user)):
        student_obj = pickle.load(open(os.path.join(settings.BASE_STUDENTS_DIR,user),rb))
        if student_obj.login(user,pwd):
            select_course(student_obj)
        else:
            print("密码错误")
    else:
        print("用户不存在")

def register(user,pwd):
    obj = models.Student()
    obj.register(user,pwd)

def main():
    inp =input("1、登录;2、注册 \n >>>")
    user  = input("用户名: ")
    pwd = input("密码: ")

    if inp == "1":
        login(user,pwd)
    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()

 


执行结果:

D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/bin/students.py
1、登录;2、注册 
 >>>1
用户名: xiaoming
密码: 123
生物 10 xiaojun
数学 4 yangling
体育 3 suoning

 

step6:删除db文件夹下的course_list文件

 

step7:修改bin文件夹下的students.py

#!/usr/bin/env python
#coding:utf8
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from lib import models
from config import settings
import pickle
from lib.models import Course
from lib.models import Admin
from lib.models import Teacher

def course_info(student_obj):
    for item in student_obj.course_list:
        print(item.course_name,item.teacher.name)


def select_course(student_obj):
    course_list = pickle.load(open(settings.COURSE_DB_DIR,rb))
    for num,item in enumerate(course_list,1):
        print(num,item.course_name,item.cost,item.teacher.name)

    while True:
        num = input("请选择课程(q表示退出): ")
        if num == "q":
            break

        selected_course_obj = course_list[int(num) -1]
        if selected_course_obj not in student_obj.course_list:
            student_obj.course_list.append(selected_course_obj)

    pickle.dump(student_obj,open(os.path.join(settings.BASE_STUDENTS_DIR,student_obj.username),"wb"))

def login(user,pwd):
    if os.path.exists(os.path.join(settings.BASE_STUDENTS_DIR,user)):
        student_obj = pickle.load(open(os.path.join(settings.BASE_STUDENTS_DIR,user),rb))
        if student_obj.login(user,pwd):
            inp = input("1、选课;2、上课;3、查看课程信息")
            if inp == "1":
                select_course(student_obj)
            elif inp == "3":
                course_info(student_obj)


        else:
            print("密码错误")
    else:
        print("用户不存在")

def register(user,pwd):
    obj = models.Student()
    obj.register(user,pwd)

def main():
    inp =input("1、登录;2、注册 \n >>>")
    user  = input("用户名: ")
    pwd = input("密码: ")

    if inp == "1":
        login(user,pwd)
    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()

 

step8:修改并执行bin包下的administrator.py

#!/usr/bin/env python
#coding:utf8

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import random
import time
import pickle

from lib import models
from config import settings
from lib.models import *

def create_course(admin_obj):
    teacher_list = pickle.load(open(settings.TEACHER_DB_DIR,rb))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = models.Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists(settings.COURSE_DB_DIR):
        exists_list = pickle.load(open(settings.COURSE_DB_DIR,rb))
        course_list.extend(exists_list)

    pickle.dump(course_list,open(settings.COURSE_DB_DIR,wb))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = models.Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists(settings.TEACHER_DB_DIR):
        exists_list = pickle.load(open(settings.TEACHER_DB_DIR,rb))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open(settings.TEACHER_DB_DIR,wb))

def login(user,pwd):
    if os.path.exists(os.path.join(settings.BASE_ADMIN_DIR,user)):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(os.path.join(settings.BASE_ADMIN_DIR,user),rb))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = models.Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()

 

执行结果:

 

D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/bin/administrator.py
1、管理员登录;2、管理员注册;
 >>>1
请输入用户名:alex
请输入密码:123
登录成功
1、创建老师;2、创建课程2
1 xiaojun 99 2017-07-12 21:24:59 alex
2 wangheng 98 2017-07-12 21:25:09 alex
3 suoning 9 2017-07-12 21:25:21 alex
4 yangling 73 2017-07-12 21:25:34 alex
请输入课程名称(q表示退出): 数学
请选择课时费:9
请选择老师(序号):1
请输入课程名称(q表示退出): 生物
请选择课时费:8
请选择老师(序号):2
请输入课程名称(q表示退出): 体育
请选择课时费:3
请选择老师(序号):2
请输入课程名称(q表示退出): q
1、创建老师;2、创建课程q

 

step9:执行bin包下的students.py

执行结果:

D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/bin/students.py
1、登录;2、注册 
 >>>1
用户名: xiaoming
密码: 123
1、选课;2、上课;3、查看课程信息1
1 数学 9 xiaojun
2 生物 8 wangheng
3 体育 3 wangheng
请选择课程(q表示退出): 1
请选择课程(q表示退出): q



D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/bin/students.py
1、登录;2、注册 
 >>>1
用户名: xiaoming
密码: 123
1、选课;2、上课;3、查看课程信息3
数学 xiaojun

 

step10:修改bin包下的students.py

 

#!/usr/bin/env python
#coding:utf8
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from lib import models
from config import settings
import pickle
from lib.models import Course
from lib.models import Admin
from lib.models import Teacher

def course_info(student_obj):
    for item in student_obj.course_list:
        print(item.course_name,item.teacher.name)


def select_course(student_obj):
    course_list = pickle.load(open(settings.COURSE_DB_DIR,rb))
    for num,item in enumerate(course_list,1):
        print(num,item.course_name,item.cost,item.teacher.name)

    while True:
        num = input("请选择课程(q表示退出): ")
        if num == "q":
            break

        selected_course_obj = course_list[int(num) -1]
        if selected_course_obj not in student_obj.course_list:
            student_obj.course_list.append(selected_course_obj)

    pickle.dump(student_obj,open(os.path.join(settings.BASE_STUDENTS_DIR,student_obj.username),"wb"))

def login(user,pwd):
    if os.path.exists(os.path.join(settings.BASE_STUDENTS_DIR,user)):
        student_obj = pickle.load(open(os.path.join(settings.BASE_STUDENTS_DIR,user),rb))
        if student_obj.login(user,pwd):
            while True:
                inp = input("1、选课;2、上课;3、查看课程信息")
                if inp == "1":
                    select_course(student_obj)
                elif inp == "3":
                    course_info(student_obj)
                else:
                    break


        else:
            print("密码错误")
    else:
        print("用户不存在")

def register(user,pwd):
    obj = models.Student()
    obj.register(user,pwd)

def main():
    inp =input("1、登录;2、注册 \n >>>")
    user  = input("用户名: ")
    pwd = input("密码: ")

    if inp == "1":
        login(user,pwd)
    elif inp == "2":
        register(user,pwd)

if __name__ == __main__:
    main()

 

 

 

 


本周作业:
选课系统

管理员:
    创建老师:姓名、性别、年龄、资产
    创建课程:课程名称、上课时间、课时费、关联老师
    使用pickle保存在文件
    
学生:
    学生:用户名、密码、性别、年龄、选课列表、上课记录
    1.列举所有课程
    2.选择课程
    3.学生上课
    4.ret = 课程.work() 获取课程的返回值;资产+课时费
    
注意:在python2版本,如果文件夹中没有__init__文件,是无法导入包的
    
作业讲解之管理员登录

step1:新建day13_homework文件夹,在该文件夹下新建administrator.py文件和lib包,lib包下新建models.py文件

step2:修改并执行administrator.py文件
#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,‘xb‘))


def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    if inp == "1":
        pass
    elif inp == "2":
        user = input("请输入用户名:")
        pwd = input("请输入密码:")
        admin_obj = Admin()
        admin_obj.register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    

    
执行结果:
1、管理员登录;2、管理员注册;
 >>>2
请输入用户名:alex
请输入密码:123


1、管理员登录;2、管理员注册;
 >>>2
请输入用户名:serven
请输入密码:123



step3:修改并执行administrator.py

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,‘xb‘))


def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    if inp == "1":
        user = input("请输入用户名:")
        pwd = input("请输入密码:")
        import os
        if os.path.exists(user):
            admin_obj = pickle.load(open(user,‘rb‘))
            if admin_obj.login(user,pwd):
                print("登录成功")
            else:
                print("登录失败")
        else:
            print("用户不存在")
        
    elif inp == "2":
        user = input("请输入用户名:")
        pwd = input("请输入密码:")
        admin_obj = Admin()
        admin_obj.register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    
###############################################

作业讲解之创建老师

step1:修改并执行administrator.py

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,‘xb‘))

def create_course(admin_obj):
    teacher_list = pickle.load(open("teacher_list",‘rb‘))
    for item in teacher_list:
        print(item.name,item.age,item.create_time,item.create_admin.username)

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists("teacher_list"):
        exists_list = pickle.load(open("teacher_list",‘rb‘))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open("teacher_list",‘wb‘))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,‘rb‘))
        if admin_obj.login(user,pwd):
                print("登录成功")
                sel = input("1、创建老师;2、创建课程")
                if sel == "1":
                    create_teacher(admin_obj)
                elif sel == "2":
                    create_course(admin_obj)
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    
执行结果:
D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/index.py
1、管理员登录;2、管理员注册;
 >>>1
请输入用户名:alex
请输入密码:123
登录成功
1、创建老师;2、创建课程1
请输入老师姓名(q表示退出) xiaojun
请输入老师年龄: 99
请输入老师姓名(q表示退出) wangheng
请输入老师年龄: 98
请输入老师姓名(q表示退出) suoning
请输入老师年龄: 9
请输入老师姓名(q表示退出) yangling
请输入老师年龄: 73
请输入老师姓名(q表示退出) q

Process finished with exit code 0


step2:修改并执行administrator.py

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,‘xb‘))

def create_course(admin_obj):
    teacher_list = pickle.load(open("teacher_list",‘rb‘))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)

    input("请输入课程名称....: ")
    input("请选择老师 1,2,3,4...:")


def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists("teacher_list"):
        exists_list = pickle.load(open("teacher_list",‘rb‘))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open("teacher_list",‘wb‘))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,‘rb‘))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    
    
step3:修改并执行administrator.py

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Course:
    """
    课程相关信息
    """
    def __init__(self,course_name,cost,teacher_obj,admin):
        self.course_name = course_name
        self.cost = cost
        self.teacher = teacher_obj
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,‘xb‘))

def create_course(admin_obj):
    teacher_list = pickle.load(open("teacher_list",‘rb‘))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists("course_list"):
        exists_list = pickle.load(open("course_list",‘rb‘))
        course_list.extend(exists_list)

    pickle.dump(course_list,open("course_list",‘wb‘))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists("teacher_list"):
        exists_list = pickle.load(open("teacher_list",‘rb‘))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open("teacher_list",‘wb‘))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,‘rb‘))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    
执行结果:
D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/index.py
1、管理员登录;2、管理员注册;
 >>>1
请输入用户名:alex
请输入密码:123
登录成功
1、创建老师;2、创建课程2
1 xiaojun 99 2017-07-12 21:24:59 alex
2 wangheng 98 2017-07-12 21:25:09 alex
3 suoning 9 2017-07-12 21:25:21 alex
4 yangling 73 2017-07-12 21:25:34 alex
请输入课程名称(q表示退出): 生物
请选择课时费:10
请选择老师(序号):1
请输入课程名称(q表示退出): 数学
请选择课时费:4
请选择老师(序号):4
请输入课程名称(q表示退出): 体育
请选择课时费:3
请选择老师(序号):3
请输入课程名称(q表示退出): q
1、创建老师;2、创建课程q

Process finished with exit code 0


###########################################


作业讲解之程序目录

step1:修改lib包下models.py

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os

class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Course:
    """
    课程相关信息
    """
    def __init__(self,course_name,cost,teacher_obj,admin):
        self.course_name = course_name
        self.cost = cost
        self.teacher = teacher_obj
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = self.username
        pickle.dump(self,open(path,‘xb‘))
        
        

step2:修改administrator.py

#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os
from lib import models

def create_course(admin_obj):
    teacher_list = pickle.load(open("teacher_list",‘rb‘))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = models.Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists("course_list"):
        exists_list = pickle.load(open("course_list",‘rb‘))
        course_list.extend(exists_list)

    pickle.dump(course_list,open("course_list",‘wb‘))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = models.Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists("teacher_list"):
        exists_list = pickle.load(open("teacher_list",‘rb‘))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open("teacher_list",‘wb‘))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,‘rb‘))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = models.Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    
    
step3:在day13_homework文件夹下新建db文件夹,把之前生成的alex、course_list、seven和teacher_list文件放到db文件夹中

step4:在day13_homework文件夹下新建config文件夹,在config文件夹中新建settings.py文件:

step5:修改settings.py文件:
#!/usr/bin/env python
#coding:utf8

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

BASE_ADMIN_DIR = os.path.join(BASE_DIR,"db","admin")

TEACHER_DB_DIR = os.path.join(BASE_DIR,"db","teacher_list")

COURSE_DB_DIR = os.path.join(BASE_DIR,"db","course_list")




step6:在db文件夹下新建admin文件夹,把alex和seven文件放到该文件夹:

step7:修改lib下的models.py
#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os
from config import settings


class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Course:
    """
    课程相关信息
    """
    def __init__(self,course_name,cost,teacher_obj,admin):
        self.course_name = course_name
        self.cost = cost
        self.teacher = teacher_obj
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd
        path = os.path.join(settings.BASE_ADMIN_DIR,self.username)
        pickle.dump(self,open(path,‘xb‘))



step8:执行administrator.py文件
#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os
from lib import models
from config import settings

def create_course(admin_obj):
    teacher_list = pickle.load(open(settings.TEACHER_DB_DIR,‘rb‘))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = models.Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists(settings.COURSE_DB_DIR):
        exists_list = pickle.load(open(settings.COURSE_DB_DIR,‘rb‘))
        course_list.extend(exists_list)

    pickle.dump(course_list,open(settings.COURSE_DB_DIR,‘wb‘))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = models.Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists(settings.TEACHER_DB_DIR):
        exists_list = pickle.load(open(settings.TEACHER_DB_DIR,‘rb‘))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open(settings.TEACHER_DB_DIR,‘wb‘))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,‘rb‘))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = models.Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    
    
执行结果:
1、管理员登录;2、管理员注册;
 >>>2
请输入用户名:ahu
请输入密码:123


step9:在day13_homework文件夹下新建bin和log文件夹,把administrator.py和students.py放到bin文件夹下:

step10:修改并执行administrator.py
#!/usr/bin/env python
#coding:utf8

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import random
import time
import pickle

from lib import models
from config import settings


def create_course(admin_obj):
    teacher_list = pickle.load(open(settings.TEACHER_DB_DIR,‘rb‘))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = models.Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists(settings.COURSE_DB_DIR):
        exists_list = pickle.load(open(settings.COURSE_DB_DIR,‘rb‘))
        course_list.extend(exists_list)

    pickle.dump(course_list,open(settings.COURSE_DB_DIR,‘wb‘))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = models.Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists(settings.TEACHER_DB_DIR):
        exists_list = pickle.load(open(settings.TEACHER_DB_DIR,‘rb‘))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open(settings.TEACHER_DB_DIR,‘wb‘))

def login(user,pwd):
    if os.path.exists(user):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(user,‘rb‘))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = models.Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    
    
执行结果:
1、管理员登录;2、管理员注册;
    

    
###############################

作业讲解之选课

step1:在db文件夹下新建students文件夹

step2:修改config文件夹下的settings.py文件:
#!/usr/bin/env python
#coding:utf8

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

BASE_ADMIN_DIR = os.path.join(BASE_DIR,"db","admin")

BASE_STUDENTS_DIR = os.path.join(BASE_DIR,"db","students")

TEACHER_DB_DIR = os.path.join(BASE_DIR,"db","teacher_list")

COURSE_DB_DIR = os.path.join(BASE_DIR,"db","course_list")

step3:修改lib包下的models.py文件:
#!/usr/bin/env python
#coding:utf8

import random
import time
import pickle
import os
from config import settings


class Teacher:
    """
    封装老师的相关信息
    """
    def __init__(self,name,age,admin):
        self.name = name
        self.age = age
        self.__assets = 0
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

    def gain(self,cost):
        """
        增加资产
        :param cost:增加数量
        :return:
        """
        self.__assets += cost

    def decrease(self,cost):
        """
        减少资产
        :param cost:减少的数量
        :return:
        """
        self.__assets -= cost

class Course:
    """
    课程相关信息
    """
    def __init__(self,course_name,cost,teacher_obj,admin):
        self.course_name = course_name
        self.cost = cost
        self.teacher = teacher_obj
        self.create_time = time.strftime("%Y-%m-%d %H:%M:%S")
        self.create_admin = admin

class Admin:
    def __init__(self):
        self.username = None
        self.password = None

    def login(self,user,pwd):
        """
        管理员登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False


    def register(self,user,pwd):
        """
        管理员注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd
        path = os.path.join(settings.BASE_ADMIN_DIR,self.username)
        pickle.dump(self,open(path,‘xb‘))

class Student:
    """
    学生相关信息
    """
    def __init__(self):
        self.username = None
        self.password = None

        self.course_list = []
        self.study_dict = {}

    def select_course(self,course_obj):
        """
        学生选课
        :param course_obj:
        :return:
        """
        self.course_obj.append(course_obj)

    def study(self,course_obj):
        """
        学生上课
        :param course_obj:
        :return:
        """
        class_result = course_obj.have_lesson()

        if course_obj in self.study_dict.keys():
            self.study_dict[course_obj].append(class_result)
        else:
                self.study_dict[course_obj] = [class_result,]

    def login(self,user,pwd):
        """
        学生登录
        :param user:
        :param pwd:
        :return:
        """
        if self.username == user and self.password == pwd:
            return True
        else:
            return False

    def register(self,user,pwd):
        """
        学生注册
        :param user:
        :param pwd:
        :return:
        """
        self.username = user
        self.password = pwd

        path = os.path.join(settings.BASE_STUDENTS_DIR,self.username)
        pickle.dump(self,open(path,"xb"))






step4:修改并执行bin文件夹下的students.py

#!/usr/bin/env python
#coding:utf8
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from lib import models
from config import settings


def register(user,pwd):
    obj = models.Student()
    obj.register(user,pwd)

def main():
    inp =input("1、登录;2、注册")
    user  = input("用户名: ")
    pwd = input("密码: ")

    if inp == "1":
        pass
    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()

执行结果:
1、登录;2、注册2
用户名: xiaoming
密码: 123


1、登录;2、注册2
用户名: xiaoli
密码: 123


step5:修改并执行bin文件夹下的students.py

#!/usr/bin/env python
#coding:utf8
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from lib import models
from config import settings
import pickle
from lib.models import Course
from lib.models import Admin
from lib.models import Teacher

def select_course(student_obj):
    course_list = pickle.load(open(settings.COURSE_DB_DIR,‘rb‘))
    for item in course_list:
        print(item.course_name,item.cost,item.teacher.name)

def login(user,pwd):
    if os.path.exists(os.path.join(settings.BASE_STUDENTS_DIR,user)):
        student_obj = pickle.load(open(os.path.join(settings.BASE_STUDENTS_DIR,user),‘rb‘))
        if student_obj.login(user,pwd):
            select_course(student_obj)
        else:
            print("密码错误")
    else:
        print("用户不存在")

def register(user,pwd):
    obj = models.Student()
    obj.register(user,pwd)

def main():
    inp =input("1、登录;2、注册 \n >>>")
    user  = input("用户名: ")
    pwd = input("密码: ")

    if inp == "1":
        login(user,pwd)
    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()
    


执行结果:
D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/bin/students.py
1、登录;2、注册
 >>>1
用户名: xiaoming
密码: 123
生物 10 xiaojun
数学 4 yangling
体育 3 suoning

step6:删除db文件夹下的course_list文件


step7:修改bin文件夹下的students.py
#!/usr/bin/env python
#coding:utf8
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from lib import models
from config import settings
import pickle
from lib.models import Course
from lib.models import Admin
from lib.models import Teacher

def course_info(student_obj):
    for item in student_obj.course_list:
        print(item.course_name,item.teacher.name)


def select_course(student_obj):
    course_list = pickle.load(open(settings.COURSE_DB_DIR,‘rb‘))
    for num,item in enumerate(course_list,1):
        print(num,item.course_name,item.cost,item.teacher.name)

    while True:
        num = input("请选择课程(q表示退出): ")
        if num == "q":
            break

        selected_course_obj = course_list[int(num) -1]
        if selected_course_obj not in student_obj.course_list:
            student_obj.course_list.append(selected_course_obj)

    pickle.dump(student_obj,open(os.path.join(settings.BASE_STUDENTS_DIR,student_obj.username),"wb"))

def login(user,pwd):
    if os.path.exists(os.path.join(settings.BASE_STUDENTS_DIR,user)):
        student_obj = pickle.load(open(os.path.join(settings.BASE_STUDENTS_DIR,user),‘rb‘))
        if student_obj.login(user,pwd):
            inp = input("1、选课;2、上课;3、查看课程信息")
            if inp == "1":
                select_course(student_obj)
            elif inp == "3":
                course_info(student_obj)


        else:
            print("密码错误")
    else:
        print("用户不存在")

def register(user,pwd):
    obj = models.Student()
    obj.register(user,pwd)

def main():
    inp =input("1、登录;2、注册 \n >>>")
    user  = input("用户名: ")
    pwd = input("密码: ")

    if inp == "1":
        login(user,pwd)
    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()


step8:修改并执行bin包下的administrator.py

#!/usr/bin/env python
#coding:utf8

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
import random
import time
import pickle

from lib import models
from config import settings
from lib.models import *

def create_course(admin_obj):
    teacher_list = pickle.load(open(settings.TEACHER_DB_DIR,‘rb‘))
    for num,item in enumerate(teacher_list,1):
        print(num,item.name,item.age,item.create_time,item.create_admin.username)
    course_list = []
    while True:
        name = input("请输入课程名称(q表示退出): ")
        if name == "q":
            break
        cost = input("请选择课时费:")
        num = input("请选择老师(序号):")
        obj = models.Course(name,cost,teacher_list[int(num) -1],admin_obj)
        course_list.append(obj)

    if os.path.exists(settings.COURSE_DB_DIR):
        exists_list = pickle.load(open(settings.COURSE_DB_DIR,‘rb‘))
        course_list.extend(exists_list)

    pickle.dump(course_list,open(settings.COURSE_DB_DIR,‘wb‘))

def create_teacher(admin_obj):
    teacher_list = []
    while True:
        teacher_name = input("请输入老师姓名(q表示退出) ")
        if teacher_name == "q":
            break
        teacher_age = input("请输入老师年龄: ")
        obj = models.Teacher(teacher_name,teacher_age,admin_obj)
        teacher_list.append(obj)

    if os.path.exists(settings.TEACHER_DB_DIR):
        exists_list = pickle.load(open(settings.TEACHER_DB_DIR,‘rb‘))
        teacher_list.extend(exists_list)

    pickle.dump(teacher_list,open(settings.TEACHER_DB_DIR,‘wb‘))

def login(user,pwd):
    if os.path.exists(os.path.join(settings.BASE_ADMIN_DIR,user)):
        #从文件中将管理员对象读取出来(里面封装了用户信息以及提供了登录方法)
        admin_obj = pickle.load(open(os.path.join(settings.BASE_ADMIN_DIR,user),‘rb‘))
        if admin_obj.login(user,pwd):
                print("登录成功")
                while True:
                    sel = input("1、创建老师;2、创建课程")
                    if sel == "1":
                        create_teacher(admin_obj)
                    elif sel == "2":
                        create_course(admin_obj)
                    else:
                        break
        else:
                return 1
    else:
        return 0

def register(user,pwd):
    admin_obj = models.Admin()
    admin_obj.register(user,pwd)

def main():
    inp = input("1、管理员登录;2、管理员注册;\n >>>")
    user = input("请输入用户名:")
    pwd = input("请输入密码:")

    if inp == "1":
        ret = login(user,pwd)
        if ret == 1:
            print("密码错误")
        elif ret == 0:
            print("用户不存在")

    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()

执行结果:
D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/bin/administrator.py
1、管理员登录;2、管理员注册;
 >>>1
请输入用户名:alex
请输入密码:123
登录成功
1、创建老师;2、创建课程2
1 xiaojun 99 2017-07-12 21:24:59 alex
2 wangheng 98 2017-07-12 21:25:09 alex
3 suoning 9 2017-07-12 21:25:21 alex
4 yangling 73 2017-07-12 21:25:34 alex
请输入课程名称(q表示退出): 数学
请选择课时费:9
请选择老师(序号):1
请输入课程名称(q表示退出): 生物
请选择课时费:8
请选择老师(序号):2
请输入课程名称(q表示退出): 体育
请选择课时费:3
请选择老师(序号):2
请输入课程名称(q表示退出): q
1、创建老师;2、创建课程q



step9:执行bin包下的students.py

执行结果:

D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/bin/students.py
1、登录;2、注册
 >>>1
用户名: xiaoming
密码: 123
1、选课;2、上课;3、查看课程信息1
1 数学 9 xiaojun
2 生物 8 wangheng
3 体育 3 wangheng
请选择课程(q表示退出): 1
请选择课程(q表示退出): q



D:\Python34\python.exe F:/PycharmProject/day13/day13_homework/bin/students.py
1、登录;2、注册
 >>>1
用户名: xiaoming
密码: 123
1、选课;2、上课;3、查看课程信息3
数学 xiaojun


step10:修改bin包下的students.py


#!/usr/bin/env python
#coding:utf8
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from lib import models
from config import settings
import pickle
from lib.models import Course
from lib.models import Admin
from lib.models import Teacher

def course_info(student_obj):
    for item in student_obj.course_list:
        print(item.course_name,item.teacher.name)


def select_course(student_obj):
    course_list = pickle.load(open(settings.COURSE_DB_DIR,‘rb‘))
    for num,item in enumerate(course_list,1):
        print(num,item.course_name,item.cost,item.teacher.name)

    while True:
        num = input("请选择课程(q表示退出): ")
        if num == "q":
            break

        selected_course_obj = course_list[int(num) -1]
        if selected_course_obj not in student_obj.course_list:
            student_obj.course_list.append(selected_course_obj)

    pickle.dump(student_obj,open(os.path.join(settings.BASE_STUDENTS_DIR,student_obj.username),"wb"))

def login(user,pwd):
    if os.path.exists(os.path.join(settings.BASE_STUDENTS_DIR,user)):
        student_obj = pickle.load(open(os.path.join(settings.BASE_STUDENTS_DIR,user),‘rb‘))
        if student_obj.login(user,pwd):
            while True:
                inp = input("1、选课;2、上课;3、查看课程信息")
                if inp == "1":
                    select_course(student_obj)
                elif inp == "3":
                    course_info(student_obj)
                else:
                    break


        else:
            print("密码错误")
    else:
        print("用户不存在")

def register(user,pwd):
    obj = models.Student()
    obj.register(user,pwd)

def main():
    inp =input("1、登录;2、注册 \n >>>")
    user  = input("用户名: ")
    pwd = input("密码: ")

    if inp == "1":
        login(user,pwd)
    elif inp == "2":
        register(user,pwd)

if __name__ == ‘__main__‘:
    main()








   

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

JAVA | 学生选课系统

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

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

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

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

选课系统作业