Python学习第72天(复习:选课实现习题)
Posted 崆峒山肖大侠
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习第72天(复习:选课实现习题)相关的知识,希望对你有一定的参考价值。
之前有一个关于选课的练习,一直没有给实现,趁着明天休息,琢磨一下给弄出来,本来以为今天能搞定,看来是我想多了。
先看一下文件分布吧
没啥可多说的,今天完成了一部分,剩下的明天搞定。
bin文件下:
import os,sys,logging,shelve BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) from conf import setting from core.main import Admin if __name__ == \'__main__\': Admin.run()
conf文件下的setting
import os,time,shelve,sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) from src.class_school import School data_path = os.path.join(BASE_DIR+\'\\db\') school_file =os.path.join(data_path,\'school\')
core文件下的main
import os,sys,logging,shelve BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) from core.school_view import School_view from core.teacher_view import Teacher_view from coer.student_view import Student_view class Admin: def run(self): exit_flag = False menu = u\'\'\' \\033[32;1m 1.学生视图 2.老师视图 3.学校视图 4.退出\\033[0m \'\'\' choice = { \'1\': Student_view , \'2\': School_view , \'3\': School_view , \'4\': exit() } while not exit_flag: print(menu) user_option = input(\'请输入你想进入的界面>>>\') if user_option in choice: choice[user_option]() else: print(\'输入指令不正确!!!请重新输入!!!\') if __name__ == \'__main__\': a = Admin() a.run()
core文件下的school_view
import os,sys,logging,shelve BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(BASE_DIR) from conf import setting from src.class_school import School class School_view: choice = { \'1\': \'add_course\', \'2\': \'add_classroom\', \'3\': \'add_teacher\', \'4\': \'show_classroom\', \'5\': \'show_teacher\', \'6\': \'add_teacher\', \'7\':exit() } def __init__(self): if os.path.exists(setting.school_file+\'.dat\'): self.school_file = shelve.open(setting.school_file) self.school_manger() self.school_file.close() else: print(\'没有学校和班级数据,请先进行创建\') self.init_school() self.school_manger() self.school_file.close() def init_school(self): self.school_file = shelve.open(setting.school_file) self.school_file[\'北京\'] = School(\'总校\',\'北京\') self.school_file[\'上海\'] = School(\'总校\',\'上海\') def school_manger(self): while True: for school_name in self.school_file: print(\'学校名称是:%s\'%school_name) school_option = input(\'请输入需要管理的学校名称\') if school_option in self.school_file: self.school_option = school_option self.school_obj = self.school_file[school_option] else: print(\'请重新输入!!!\') # \'\'\' 如果是北京就等于self.school_obj[北京] # file[北京] =School(\'总校\',\'北京\') # 所以当下文出现了self.school_obj.school_course =School(\'总校\',\'北京\').school_course # 等同于调用了School().school_course的属性是 # \'\'\' while True: menu = \'1.添加课程\' \\ \'2.添加班级\' \\ \'3.添加讲师\' \\ \'4.查看班级、课程\' \\ \'5.查看讲师 \' \\ \'6.修改讲师\' \\ \'7.退出\' print(\'欢迎来到Python学习%s校区\'%self.school_option) print(menu) user_choice = input(\'请选择你所需的功能>>>\') if hasattr(self,choice[user_choice]): func = getattr(self,choice[user_choice]) else: print(\'指令输入错误。\') pass def add_course(self): course_name = input(\'请输入课程名称>>>:\').strip() course_price = input(\'请输入课程价格>>>:\').strip() course_time = input(\'请输入课程周期[默认为周]>>>:\').strip() if course_name in self.school_obj.school_course: print(\'课程已经存在\') else: self.school_obj.create_course(course_name, course_price, course_time) # 相当于School.create_course(course_name, course_price, course_time) print(\'%s课程添加成功\' % course_name) self.school_file.update({self.school_option: self.school_obj}) self.school_file.close() def add_classroom(self): class_name = input(\'请输入班级名称>>>:\').strip() class_course = input(\'请输入课程>>>:\').strip() if class_name not in self.school_obj.sch_class_room: # sch_class_room if class_name not in self.school_obj.school_course: course_obj = self.school_obj.school_course[class_course] self.school_obj.create_classroom(class_name, course_obj) self.school_file.update({self.school_option: self.school_obj}) self.school_file.close() print(\'班级创建成功\') else: print(\'课程不存在\') else: print(\'班级已经存在\') def show_classroom(self): for classroom in self.school_obj.sch_class_room: class_obj = self.school_obj.sch_class_room[classroom] print(\'班级名称:%s\\t课程:%s\' % (class_obj.class_name, class_obj.course_obj.course_name)) def add_teacher(self): teacher_name = input(\'请输入讲师姓名>>>:\') teacher_age = input(\'请输入讲师年龄>>>:\') teacher_gender = input(\'请输入讲师性别>>>:\') teacher_salary = input(\'请输入讲师薪水>>>:\') class_name = input(\'请输入授课班级>>>:\') if class_name in self.school_obj.sch_class_room: # sch_class_room class_obj = self.school_obj.sch_class_room[class_name] if teacher_name not in self.school_obj.sch_teacher: self.school_obj.create_teacher(teacher_name, teacher_age, teacher_gender, teacher_salary, class_name, class_obj) print(\'讲师创建成功,请确认是否已经签完合同\') else: self.school_obj.modify_teacher({}) print(\'修改讲师成功\') self.school_file.update({self.school_option: self.school_obj}) self.school_file.close() else: print(\'请先创建班级\') def show_teacher(self): for teacher_name in self.school_obj.sch_teacher: teacher_obj = self.school_obj.sch_teacher[teacher_name] for t in teacher_obj.teacher_classroom: class_obj = self.school_obj.sch_class_room[t] stu_list = [] for j in class_obj.class_student: stu_list.append(j) print(\'教师姓名:%s\\t教师所在班级:%s\\t教授课程:%s\\t课程学员:%s\' % (teacher_obj.teacher_name , t, class_obj.course_obj.course_name, class_obj.class_student)) def exit(self): sys.exit(\'程序退出\')
关于logging模块、shelve模块,基本忘的差不多了,现在内容逐渐多了,复习貌似更重要,不能过于激进,争取端午节前实现自己的承诺。
以上是关于Python学习第72天(复习:选课实现习题)的主要内容,如果未能解决你的问题,请参考以下文章