python学生选课系统
校园管理系统(02) 需求: 从“学生选课系统” 这几个字就可以看出来,我们最核心的功能其实只有 选课。 角色: 学生、管理员 功能: 登陆 : 管理员和学生都可以登陆,且登陆之后可以自动区分身份 选课 : 学生可以自由的为自己选择课程 创建用户 : 选课系统是面向本校学生的,因此所有的用户都应该由管理员完 成 查看选课情况 :每个学生可以查看自己的选课情况,而管理员应该可以查看 所有学生的信息 工作流程: 登陆 :用户输入用户名和密码 判断身份 :在登陆成果的时候应该可以直接判断出用户的身份 是学生、讲师 还是管理员 学生用户 :对于学生用户来说,登陆的工作几乎不变 1、查看所有课程 2、选择课程 3、查看所选课程 4、退出程序 管理员用户:管理员用户也可以做更多的事情 1、创建课程 2、创建学生学生账号 3、查看所有课程 4、查看所有学生 5、查看所有学生的选课情况 # 讲师进阶需求 6、创建讲师 7、为讲师指定班级 8、创建班级 9、为学生指定班级 10、退出程序 讲师用户 :对于讲师用户来说,可以完成的功能如下 1、查看所有课程 2、查看所教班级 3、查看班级中的学生 4、退出程序
学生选课流程
readme文件信息
import os import sys base_path = os.path.dirname(os.path.dirname(__file__)) sys.path.append(base_path) from core import cores if __name__ == '__main__': cores.start()
userinfo = r'D:\python_23\day07\db\userinfo' courseinfo = r'D:\python_23\day07\db\courseinfo' select_course = r'D:\python_23\day07\db\select_course' gradeinfo = r'D:\python_23\day07\db\gradeinfo' tearch_grade = r'D:\python_23\day07\db\tearch_grade' student_grade = r'D:\python_23\day07\db\student_grade'
#登录 import hashlib def md5(arg): # 这是加密函数,将传进来的函数加密 md5_pwd = hashlib.md5(bytes('123', encoding='utf-8')) md5_pwd.update(bytes(arg, encoding='utf-8')) return md5_pwd.hexdigest() # 返回加密的数据 from conf import settings def login(): usr = input('please user:') pwd = input('please password: ') with open(settings.userinfo,'r') as f: for line in f: user,passwd,identify = line.strip().split('|') if user == usr and passwd == md5(pwd): print('登录成功') return {'user':user,'identify':identify} else: print('登录失败,输入正确的账号密码') return {'user': user, 'identify': None}
#登录 import sys import os from core import auth from conf import settings import pickle import hashlib def md5(arg): # 这是加密函数,将传进来的函数加密 md5_pwd = hashlib.md5(bytes('123', encoding='utf-8')) md5_pwd.update(bytes(arg, encoding='utf-8')) return md5_pwd.hexdigest() # 返回加密的数据 class Die: def show_select_courese(self): with open(settings.courseinfo,'rb') as f: while True: try: obj = pickle.load(f) print('课程名字:%s,课程价格:%s,课程周期:%s,教导老师:%s' % (obj.name, obj.price, obj.period, obj.tearch)) except EOFError: break class Course: def __init__(self,name,price,period,tearch): self.name = name self.price = price self.period = period self.tearch = tearch class Student: operat_list = [('查看可选课程','show_courese'),('选择课程','choose_course'),('查看已选课程','show_select_courese'),('退出','exit')] courses_list1 = [] def __init__(self,name): self.name = name def show_courese(self): Die.show_select_courese(self.name) def choose_course(self): Die.show_select_courese(self.name) with open(settings.select_course, 'ab') as f1: cours = input('请选择要学习课程名字:') if cours in Student.courses_list1: print('课程已存在') else: Student.courses_list1.append(cours) pickle.dump({self.name: Student.courses_list1}, f1) print('用户%s,添加课程%s成功' % (self.name, cours)) # pickle.dump({ret['user']:Student.courses_list1},f1) # print('用户%s,添加课程%s成功'%(ret['user'],cours)) def show_select_courese(self): with open(settings.select_course, 'rb') as f1: while True: try: obj = pickle.load(f1) print(obj) except EOFError: break def exit(self): exit('欢迎再来') class Manager(Die): operat_list = [('创建课程', 'create_course'), ('创建学生', 'create_student'), ('查看可选课程', 'show_select_course'), ('查看所有学生', 'show_select_student'), ('查看所有学生和已选课程', 'show_stu_course'), ('创建讲师', 'create_tearch'), ('为讲师指定班级', 'tearch_grade'), ('创建班级', 'create_grade'), ('为学生指定班级', 'select_stu_grade'), ('退出', 'exit')] def __init__(self,name): self.name = name def create_course(self): name = input('请输入课程:') price = input('价格:') period = input('周期:') tearch = input('tearch:') course = Course(name,price,period,tearch) with open(settings.courseinfo,'ab') as f: pickle.dump(course,f) print('课程创建成功') def create_student(self): user = input('请输入创建的学生名字: ') pwd = input('请输入密码: ') ret = input('输入学生的类别,例如Student:') if ret == 'Student': course = '\n%s|%s|%s'%(user,md5(pwd),ret) with open(settings.userinfo,'a') as f: f.write('%s'%course) print('%s创建成功'%user) else: print('您输入的不是Student') def show_select_course(self): Die.show_select_courese(self.name) def show_select_student(self): with open(settings.userinfo,'r') as f: for i in f: name,pwd,identify = i.strip().split('|') if identify == 'Manager' or identify == 'Tearch': pass else: print(name) def show_stu_course(self): with open(settings.select_course, 'rb') as f1: while True: try: obj = pickle.load(f1) for x,y in obj.items(): print('用户%s一共学习了%s课程'%(x,y)) # print(obj) except EOFError: break def create_tearch(self): user = input('请输入创建的讲师名字: ') pwd = input('请输入密码: ') ret = input('输入讲师的类别,例如Tearch:') if ret == 'Tearch': course = '\n%s|%s|%s'%(user,md5(pwd),ret) with open(settings.userinfo,'a') as f: f.write('%s'%course) print('%s老师创建成功'%user) else: print('您输入的不是Student') def tearch_grade(self): grade_list = [] with open(settings.userinfo,'r') as f: for line in f: name,pwd,identify = line.strip().split('|') with open(settings.gradeinfo,'rb') as f1: obj = pickle.load(f1) grade_list.append(obj) if identify == 'Tearch': print('可以为%s老师分配班级有\n(%s)班'%(name,obj)) print('-------------') grade = int(input('请输入班级号')) with open(settings.tearch_grade,'ab') as f2: pickle.dump({name:grade_list},f2) print('%s老师已经为%s班的讲师'%(name,grade)) def create_grade(self): grade = input('请输入您要创建的班级号') with open(settings.gradeinfo,'ab') as f: pickle.dump(grade,f) print('%s号班级已经创建成功'%grade) def select_stu_grade(self): stu_grade = [] with open(settings.userinfo,'r') as f: with open(settings.gradeinfo, 'rb') as f1: obj = pickle.load(f1) for line in f: name, pwd, identify = line.strip().split('|') if identify == 'Manager' or identify == 'Tearch': pass else: print('请为下面(%s)学生选择班级,\n可选班级有(%s)号班级\n'%(name,obj)) ret = input('请输入要选择的班级号') # stu_grade.append('ret') with open(settings.student_grade,'ab') as f3: pickle.dump({name:ret},f3) print('学生%s已经加入到%s班级'%(name,ret)) def exit(self): exit('欢迎下次再来') class Tearch(Die): operat_list = [('查看所有课程', 'select_course'), ('查看所教班级', 'select_grade'), ('查看班级中学生', 'select_grade_stu'), ('退出', 'exit')] def __init__(self,name): self.name = name def select_course(self): Die.show_select_courese(self.name) def select_grade(self): with open(settings.tearch_grade,'rb') as f: obj = pickle.load(f) for x,y in obj.items(): print('%s老师交的班级有%s'%(x,y)) def select_grade_stu(self): with open(settings.student_grade,'rb') as f: obj = pickle.load(f) for x,y in obj.items(): print('%s号班级里一共有%s位学生'%(y,x)) def start(): ret = auth.login() if ret['identify']: cls = getattr(sys.modules[__name__],ret['identify']) obj = cls(ret['user']) while True: for index,i in enumerate(cls.operat_list,1): print(index,i[0]) ind = int(input('please num')) print('----------------') func = cls.operat_list[ind-1][1] getattr(obj,func)() print('----------------')
db下的文件
courseinfo ##课程名字数据文件 gradeinfo ##班级号数据文件 select_course ##选课数据文件 student_grade ##学生所在班级数据文件 tearch_grade ##老师所在班级数据文件 userinfo ##存放用户密码和身份的数据文件 admin|4297f44b13955235245b2497399d7a93|Manager
log暂时没放