作业要求:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格,通过学校创建课程
4. 通过学校创建班级, 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校,
6. 提供两个角色接口
6.1 学员视图, 可以注册, 交学费, 选择班级,
6.2 讲师视图, 讲师可管理自己的班级, 上课时选择班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
1 import pickle 2 3 class School(object): 4 def __init__(self, addr): 5 self.addr = addr 6 7 def hire_teacher(self): 8 ‘‘‘ 9 保存老师信息 10 :return: 11 ‘‘‘ 12 teacher_dict = pickle.load(open(‘teacher‘, ‘rb‘)) 13 T = 0 14 while T>=0: 15 name = input(‘请输入老师名‘) 16 for key in teacher_dict: 17 if key == name: 18 print(‘此老师已存在,请重新输入‘) 19 T+=1 20 T-=1 21 age = input(‘请输入年龄‘) 22 print(‘创建成功‘) 23 teacher = Teacher(name, age, self.addr) 24 teacher_dict[name] = teacher 25 pickle.dump(teacher_dict, open(‘teacher‘, ‘wb‘)) 26 27 def create_course(self): 28 ‘‘‘ 29 保存课程信息 30 :return: 31 ‘‘‘ 32 course_dict = pickle.load(open(‘course‘, ‘rb‘)) 33 name = input(‘请输入课程名‘) 34 money = input(‘请输入课程价钱‘) 35 cycle = input(‘请输入课程周期‘) 36 course_dict[name] = Course(name, money, cycle) 37 pickle.dump(course_dict, open(‘course‘, ‘wb‘)) 38 39 def create_class(self): 40 ‘‘‘ 41 创造班级,关联老师和课程 42 :return: 43 ‘‘‘ 44 teacher_dict = pickle.load(open(‘teacher‘, ‘rb‘)) 45 course_dict = pickle.load(open(‘course‘, ‘rb‘)) 46 class_dict = pickle.load(open(‘class‘, ‘rb‘)) 47 48 name = input(‘请输入班级名:‘) 49 for key in teacher_dict: 50 print(key) 51 teacher = input(‘请选择上课老师‘) 52 for key in course_dict: 53 print(key) 54 course = input(‘请输入对应课程‘) 55 if teacher in class_dict: 56 class_dict [teacher].append(Classroom(name, course_dict[course], teacher_dict[teacher])) 57 else: 58 class_dict [teacher] = [Classroom(name, course_dict[course], teacher_dict[teacher])] 59 print(‘创建成功‘) 60 pickle.dump(class_dict, open(‘class‘, ‘wb‘)) 61 62 63 class Course (object): 64 def __init__(self, course, money, cycle): 65 self.course = course 66 self.money = money 67 self.cycle = cycle 68 69 def show_course(self): 70 print(‘课程名:%s; 价钱: %s; 上课周期:%s‘ % (self.course, self.money, self.cycle)) 71 72 class Classroom(object): 73 def __init__(self, name, course, teacher): 74 self.name = name 75 self.course = course 76 self.student = [] 77 self.teacher = teacher 78 79 def show_classroom(self): 80 self.teacher.show_teacher() 81 print (‘班级名: %s‘ % self.name) 82 self.course.show_course() 83 84 85 class Student(object): 86 def __init__(self, name): 87 self.name = name 88 self.account = 0 89 self.sorce = 0 90 self.course = [] 91 92 93 94 class Teacher(object): 95 def __init__(self, name, age, addr): 96 self.name = name 97 self.age = age 98 self.addr = addr 99 100 def show_teacher(self): 101 print(‘‘‘老师姓名:%s \n老师年龄: %s\n老师所在学校:%s‘‘‘ % (self.name, self.age,self.addr)) 102 103 104 def set_student(): 105 ‘‘‘ 106 学生登陆或注册,选课 107 :return: 108 ‘‘‘ 109 class_dict = pickle.load(open(‘class‘, ‘rb‘)) 110 student_dict = pickle.load(open(‘student‘, ‘rb‘)) 111 name = input(‘请输入学生姓名‘).capitalize() 112 obj = Student(name) 113 if name in student_dict: 114 print (‘欢迎 %s‘ % name) 115 else: 116 print(‘注册成功‘) 117 while True: 118 for key in class_dict: 119 a = class_dict[key] 120 a[-1].show_classroom() 121 print(‘*‘ *30) 122 while True: 123 b = input(‘请选择课程对应的老师‘).capitalize() 124 if b in class_dict: 125 c = class_dict[b][-1] 126 break 127 else: 128 print(‘没有该老师,请重新选择‘) 129 k = input(‘请确认付费,y = yes, 其他键 = 返回, q = 退出‘).lower() 130 if k == ‘y‘: 131 while True: 132 if obj.account >= int(c.course.money): 133 obj.account -= int(c.course.money) 134 print(‘扣费成功‘) 135 obj.course.append(c) 136 c.student.append(Student(name)) 137 y = input(‘请选择:1 继续选课, 其他键 = 退出‘) 138 if y == ‘1‘: 139 break 140 else: 141 obj.course.append(c) 142 student_dict[name] = obj 143 pickle.dump(class_dict, open(‘class‘, ‘wb‘)) 144 pickle.dump(student_dict, open(‘student‘, ‘wb‘)) 145 exit() 146 else: 147 charge = input(‘余额不足,y = 充值,q = 退出, 其他键返回‘).lower() 148 if charge == ‘y‘: 149 mk = input(‘请输入充值金额‘) 150 obj.account += int(mk) 151 elif charge == ‘q‘: 152 exit() 153 else: 154 break 155 elif k == ‘q‘: 156 exit() 157 else: 158 continue 159 160 def check_student(name): 161 ‘‘‘ 162 老师上课,查看学生,或修改成绩 163 :param name: 164 :return: 165 ‘‘‘ 166 167 class_dict = pickle.load(open(‘class‘, ‘rb‘)) 168 a = class_dict[name] 169 170 for k, i in enumerate(a,1): 171 print(k) 172 print(‘班级名:%s\n课程:%s‘% (i.name, i.course.course)) 173 print ("*" * 60) 174 c = input(‘请选择班级对应的数字‘) 175 while True: 176 if int(c)>0 and int(c)<=len(a): 177 e = input(‘请选择:1 上课; 2 查看班级成员,3 修改学员成绩,其他键 = 退出‘) 178 if e == ‘1‘: 179 print (‘开始上课...‘) 180 elif e == ‘2‘: 181 d = a[int(c)-1].student 182 for o in d: 183 print (‘学生姓名 %s‘ % o.name) 184 elif e == ‘3‘: 185 d = a[int(c)-1].student 186 for n, j in enumerate(d, 1): 187 print (‘%s. 学生姓名 %s, 分数 %s‘ % (n, j.name, j.sorce)) 188 f = input(‘请选择学生的对应数字‘) 189 if int(f)>0 and int(f)<=len(d): 190 sorce = input(‘修改的分数为‘) 191 d[int(f)-1].sorce = sorce 192 pickle.dump(class_dict,open(‘class‘, ‘wb‘)) 193 else: 194 print (‘输入有误‘) 195 196 def manage(): 197 ‘‘‘ 198 管理员菜单 199 :return: 200 ‘‘‘ 201 while True: 202 b = input(‘输入学校地址: 北京; 上海‘) 203 if b == ‘北京‘ or b ==‘上海‘: 204 obj = School(b) 205 break 206 else: 207 print(‘没有这地址‘) 208 continue 209 while True: 210 a = input(‘请选择: 1 创建老师, 2 创建课程, 3 创建班级, 4 返回‘) 211 if a == ‘1‘: 212 obj.hire_teacher() 213 elif a == ‘2‘: 214 obj.create_course() 215 elif a == ‘3‘: 216 obj.create_class() 217 elif a==‘4‘: 218 break 219 else: 220 print (‘输入有误‘) 221 222 def main(): 223 ‘‘‘ 224 主菜单 225 :return: 226 ‘‘‘ 227 while True: 228 a = input(‘请选择: 1 学生; 2 老师, 3 管理员, 其他键 退出‘) 229 if a == ‘1‘: 230 set_student() 231 elif a == ‘2‘: 232 name = input(‘请输入老师姓名‘).capitalize() 233 teacher_dict = pickle.load(open(‘teacher‘, ‘rb‘)) 234 if name in teacher_dict: 235 check_student(name) 236 else: 237 print (‘不是我们学校的老师,请重新输入‘) 238 elif a == ‘3‘: 239 manage() 240 else: 241 exit() 242 243 244 245 main()
时间: 2024-10-16 10:40:12