首页 > 代码库 > python之选课系统详解[功能未完善]

python之选课系统详解[功能未完善]

作业需求

 

 技术分享

 

 

思路:1.先写出大体的类,比如学校类,学生类,课程类……

     2.写出类里面大概的方法,比如学校类里面有创建讲师、创建班级……

   3.根据下面写出大致的代码,并实现其功能

         技术分享

 

遇到的困难:

1.在类与类关联上卡住了,比如:

老师如何查看班级信息??
老师有班级名称的属性, 而要查看班级信息,需要班级对象
那应该将老师的班级名称与班级对象相关联起来
那不同老师怎么办??
所以学校创建 老师对象时,应该将老师名称与老师对象相关联起来
通过输入老师名称即可找到老师对象

 

 2.

想把讲师对象通过与讲师名字相关联成一个字典,写到文件中,但是输出时却不成功
应该是程序关序时,内存中的对象被回收了,而文件中的只是被回收对象的字符串形式
输出时找不到内存中的对象了
解决方法,看来得把讲师的相关信息写到文件中才行。

 

3.

通过Json将讲师信息成功写到文件中,但若再创建一个,后一个会把前一个讲师的信息覆盖!!

 

技术分享

解决方法:

解决方法:学校创建 一个teachers = {}字典,创建讲师时以讲师名为key,讲师信息为value,
将字典元素写到teachers字典

 

4.

写到文件中是,会出现unicode编码,即使加上encoding = utf-8也还是会

技术分享

解决方法:

python的json.dumps方法默认会输出成这种格式"\u535a\u5ba2\u56ed",。
要输出中文需要指定ensure_ascii参数为False,如下代码片段:
json.dumps({text:"中文"},ensure_ascii=False,indent=2)

来自 <http://www.cnblogs.com/zdz8207/p/python_learn_note_26.html> 

改正后文件如下:

技术分享

 

5.

问题:做了下面 的测试,如果讲师名字相同,则后一个会把前一个覆盖

 

技术分享

 

解决:但不同学校按理说是可以创建相同名字的讲师,但我这里只要讲师名字相同就不能再创建,会覆盖的。
这里我就不优化了……

 

6.

问题:运行时字典长度变化了,我擦,还有这种BUG,我太年轻了

技术分享

 

分析原因:continue只是跳出了for 循环,不是跳出while循环,具体的我现在没有例子了……

 

 7.

问题:可以查看所有学生的信息,按理说老师只能查看对应班级的学生信息才行,这里没优化……

 

 

源代码:

技术分享
  1 #选课系统
  2 import sys
  3 import json
  4 
  5 class School(object):   #创建学校类
  6 
  7 
  8     def __init__(self, school_name, address):
  9         self.school_name = school_name
 10         self.address = address
 11 
 12     def create_class(self):   #创建班级
 13         print("欢迎创建班级".center(50, -))
 14         classroom_name = input("请输入班级名称:")
 15         classroom_period = input("请输入班级周期:")
 16         classroom_obj = Classroom(classroom_name, classroom_period, choice_school_obj.school_name)  # 班级的实例
 17         print("班级成功创建,班级信息如下".center(50, -))
 18         classrooms[classroom_name] = classroom_obj  # 将班级名与班级对象相关联
 19         classroom_obj.show_classroom_info()
 20 
 21     def hire_teacher(self):  #雇讲师
 22         print("欢迎雇用讲师".center(50, -))
 23         # print("已有讲师Name:%s, Course:%s, Classroom:%s")
 24         teacher_name = input("请输入讲师名字:")
 25         teacher_sex = input("请输入讲师性别:")
 26         teacher_age = input("请输入讲师年龄:")
 27         teacher_course = input("请输入讲师对应课程:")
 28         teacher_classroom = input("请输入讲师对应班级:")
 29         teacher = Teacher(teacher_name, teacher_sex, teacher_age, teacher_course,
 30                           teacher_classroom, choice_school_obj.school_name)  # 实例化讲师对象
 31         teacher_dict = {"teacher_shcool_name": teacher.teacher_school_name, "teacher_sex": teacher_sex,
 32                         "teacher_age": teacher_age, "teacher_course": teacher_course,
 33                         "teacher_classroom": teacher_classroom}  # 用字典来存放讲师信息
 34 
 35         teacher.show_teacher_info()
 36         # 通过json将讲师的字典反序列化到dic字典中
 37 
 38         if not dic:  # 字典如果为空
 39             dic[teacher_name] = teacher_dict  # 将讲师名与讲师对象相关联
 40             # 通过json将讲师的字典序列化到teacher_文件中
 41             json.dump(dic, open("teacher_db", "w", encoding=utf-8),
 42                       ensure_ascii=False, indent=2)
 43         else:  # 如果文件中已有讲师信息
 44             if dic.get(teacher_name):  # 字典中不存在key,则返回none,不曝错
 45                 print("%s讲师已存在,不能创建名字相同的讲师" % teacher_name)
 46                 flag = True
 47             elif not dic.get(teacher_name):
 48                 dic[teacher_name] = teacher_dict
 49                 json.dump(dic, open("teacher_db", "w", encoding=utf-8), ensure_ascii=False, indent=2)
 50 
 51 
 52     def create_course(self):  #创建课程
 53         print("迎欢创建课程".center(50, -))
 54         course_type = input("请输入课程类型[eg:技术/教育/自然科学/艺术...]:")
 55         course_name = input("请输入课程名称:")
 56         course_price = input("请输入课程价格:")
 57         course_period = input("请输入课程周期:")
 58         course = Course(course_type, course_name, course_price, course_period, choice_school_obj.school_name)
 59         print("课程成功创建,课程信息如下".center(50, -))
 60         courses_dict[course_name] = course  # 将课程名与课程对象相关联
 61         course.show_course_info()
 62 
 63 
 64 class Course(object):       #课程类
 65 
 66     def __init__(self, course_type, course_name, course_price, course_period, course_place):
 67         self.course_type = course_type
 68         self.course_name = course_name
 69         self.course_price = course_price
 70         self.course_period = course_period
 71         self.course_place = course_place
 72 
 73     def show_course_info(self):
 74         print("课程类型:%s,名称:%s,价格:%s,周期:%s" % (self.course_type,self.course_name,
 75                                                          self.course_price, self.course_period))
 76 
 77 
 78 
 79 class Classroom(object):     #班级类
 80     def __init__(self, classroom_name, classroom_period, classroom_school_name):
 81         self.classroom_name = classroom_name
 82         self.classroom_period = classroom_period
 83         self.classroom_school_name = classroom_school_name
 84 
 85     def show_classroom_info(self):
 86         print("班级名称:%s\n班级周期:%s" % (self.classroom_name, self.classroom_period))
 87 
 88 
 89 class SchoolMember(object):   # 学校成员类(学生/老师)
 90 
 91     def __init__(self, member_name, member_sex, member_age):
 92         self.member_name = member_name
 93         self.member_sex = member_sex
 94         self.member_age = member_age
 95 
 96 
 97 class Student(SchoolMember):   #创建学生类(继承学校成员类)
 98 
 99 
100     def __init__(self, stu_school, stu_name, stu_sex, stu_age, stu_id, stu_course, course_price):
101         super(Student, self).__init__(stu_name, stu_sex, stu_age)
102         self.stu_school = stu_school
103         self.stu_id = stu_id
104         self.stu_course = stu_course
105         self.course_price = course_price
106 
107     def show_student_info(self):
108         print("""
109         ---------------学生%s的信息--------------
110         Name:%s
111         School:%s
112         Sex:%s
113         Age:%s
114         ID:%s
115         Course:%s
116         Course_price:%s
117         """ % (stu1.member_name,stu1.member_name,stu1.stu_school,stu1.member_sex,
118                stu1.member_age, stu1.stu_id, stu1.stu_course,stu1.course_price))
119 
120 
121 
122 class Teacher(SchoolMember):  #讲师类
123 
124     def __init__(self, teacher_name, teacher_sex, teacher_age, teacher_course, teacher_classroom, teacher_school_name):
125         super(Teacher, self).__init__(teacher_name, teacher_sex, teacher_age)
126         self.teacher_course = teacher_course
127         self.teacher_classroom = teacher_classroom
128         self.teacher_school_name = teacher_school_name
129 
130     def show_teacher_info(self):
131         print("""
132         -------------讲师%s的信息-------------
133         Name:%s
134         Sex:%s
135         Age:%s
136         Course:%s
137         Classroom:%s
138         School_name:%s
139         """ % (self.member_name, self.member_name, self.member_sex, self.member_age,
140                                    self.teacher_course, self.teacher_classroom, self.teacher_school_name))
141 
142     def show_classroom(self, te_name):    #查看班级信息   传一个班级对象,通过对象查看班级信息
143         class_room = Classroom(teachers_dict[te_name].teacher_classroom,
144                                courses_dict[teachers_dict[te_name].teacher_course].course_period,
145                                choice_school_obj.school_name)
146         class_room.show_classroom_info()
147 
148     def show_student(self):     #查看学生信息    传一个学生对象,通过对象查看学生信息
149         stu_name = input("请输入要查看学生名字:")
150         stu_dict[stu_name].show_student_info()
151 
152 
153 def stu_regiest():  # 学生注册方法,目的是为了生成学生对象
154     global stu1          #定义学生变量为全局变量
155     stu_name = input("请输入学生姓名:")
156     stu_sex = input("请输入学生性别:")
157     stu_age = input("请输入学生年龄:")
158     stu_id = input("请输入学生序号")
159     print("1.%s[%sRMB], 2.%s[%sRMB], 3.%s[%sRMB], 4.返回" % (course1.course_name, course1.course_price,
160                                                            course2.course_name, course2.course_price,
161                                                            course3.course_name, course3.course_price))
162     while True:
163         course_num = input("请选择课程:")
164         if course_num == 1:
165             stu_course = course1.course_name
166             stu1 = Student(choice_school_obj.school_name, stu_name, stu_sex,
167                            stu_age, stu_id, stu_course,course1.course_price)
168             stu_dict[stu_name] = stu1
169             break
170         elif course_num == 2:
171             stu_course = course2.course_name
172             stu1 = Student(choice_school_obj.school_name, stu_name, stu_sex,
173                            stu_age, stu_id, stu_course, course2.course_price)
174             stu_dict[stu_name] = stu1
175             break
176         elif course_num == 3:
177             stu_course = course3.course_name
178             stu1 = Student(choice_school_obj.school_name, stu_name, stu_sex,
179                            stu_age, stu_id, stu_course, course3.course_price)
180             stu_dict[stu_name] = stu1
181             break
182         elif course_num == 4:
183             break
184         else:
185             continue
186     stu1.show_student_info()
187 
188 
189 def students_view():      #学员视图
190     while True:
191         print("1.欢迎注册\n"
192               "2.返回\n"
193               "3.退出")
194         num = input("请选择:")
195         if num == 1:
196             stu_regiest()        #调用学生注册方法并生成学生对象
197         elif num == 2:
198             break
199         elif num == 3:
200             sys.exit()
201         else:
202             continue
203 
204 
205 def teacher_view():     #讲师视图
206     name = input("请输入讲师姓名:")
207     while True:
208         if dic.get(name) or teachers_dict.get(name):
209             print("欢迎%s讲师".center(50, -) % name)
210         elif not dic.get(name) and not teachers_dict.get(name):
211             print("%s讲师不存在" % name)
212             break
213         print("1.查看班级\n"
214               "2.查看学员信息\n"
215               "3.返回\n"
216               "4.退出")
217         print("功能未完善,只能输入Alex,cheng")
218         num = input("请选择:")
219         if num == 1:
220             if teachers_dict.get(name):
221                 teachers_dict[name].show_classroom(name)    #查看班级信息
222             else:
223                 print("功能未完善,只能输入Alex,cheng")
224         elif num == 2:
225             if teachers_dict.get(name):
226                 teachers_dict[name].show_student()        #查看学生信息
227             else:
228                 print("功能未完善,只能输入Alex,cheng")
229 
230         elif num == 3:
231             break
232         elif num == 4:
233             sys.exit()
234         else:
235             continue
236 
237 
238 def school_view():      #学校视图
239     flag = False
240     while not flag:
241         print("1.创建班级\n"
242               "2.创建课程\n"
243               "3.雇用讲师\n"
244               "4.返回")
245         num = input("请选择:")
246         if num == 1:
247             choice_school_obj.create_class()
248         elif num == 2:
249             choice_school_obj.create_course()
250         elif num == 3:
251             choice_school_obj.hire_teacher()
252         elif num == 4:
253             flag = True
254         else:
255             continue
256 
257 
258 def main():
259     global dic  # 全局变量
260     global choice_school_obj
261     dic = {}
262 
263     while True:
264         print("请选择学校".center(50, *))
265         choice_school = input("1.%s, 2.%s, 3.返回, 4.退出" % (s1.school_name, s2.school_name))
266         if choice_school == 1:
267             choice_school_obj = s1  # 将对象引用传给choice_school
268         elif choice_school == 2:
269             choice_school_obj = s2
270         elif choice_school == 3:
271             break
272         elif choice_school == 4:
273             sys.exit()
274         else:
275             continue
276         while True:
277             print("1.学员视图\n"
278                   "2.讲师视图\n"
279                   "3.学校管理视图\n"
280                   "4.返回\n"
281                   "5.退出")
282             num = input("请选择视图:")
283 
284             if num == 1:
285                 print("欢迎进入学员视图".center(50, *))
286                 students_view()
287             elif num == 2:
288                 print("欢迎进入讲师视图".center(50, *))
289                 teacher_view()
290             elif num == 3:
291                 print("欢迎进入学校管理视图".center(50, *))
292                 school_view()
293             elif num == 4:
294                 break
295             elif num == 5:
296                 sys.exit()
297             else:
298                 continue
299 
300 
301 
302 
303 if __name__ == __main__:
304     classrooms = {}
305     teachers_dict = {}
306     courses_dict = {}
307     stu_dict = {}
308     s1 = School("老男孩", "北京")      #实例化学校
309     s2 = School("拼客科技", "广州大学城")
310     course1 = Course("技术", "Linux", "11800", "1 year", "北京")
311     course2 = Course("技术", "Python", "6400", "7 month", "北京")   #实例化三个课程
312     course3 = Course("技术", "CCIE", "2400", "4 month", "广州大学城")
313     courses_dict["Linux"] = course1
314     courses_dict["Python"] = course2
315     courses_dict["CCEI"] = course3
316     t1 = Teacher("Alex", "M", "33", "Python", "S13", "Oldboy")
317     t2 = Teacher("cheng", "M", "35", "CCIE", "魔鬼训练营", "Pinginglab")    #实例化两个讲师
318     teachers_dict["Alex"] = t1
319     teachers_dict["cheng"] = t2
320     teacher_dict = {"teacher_name": "Alex", "teacher_sex": "M", "teacher_age": "33",
321                     "teacher_course": "Python", "teacher_classroom": "S13"}
322     teacher_dict = {"teacher_name": "Eric", "teacher_sex": "M", "teacher_age": "35",
323                     "teacher_course": "Python", "teacher_classroom": "S14"}
324     print(s1,s2)
325     main()
View Code

 

输出测试:

技术分享
  1 C:\Python34\python3.exe C:/Users/Administrator/PycharmProjects/laonanhai/xuanke_system/day7_homework.py
  2 <__main__.School object at 0x0000000002CDF160> <__main__.School object at 0x0000000002CDF198>
  3 **********************请选择学校***********************
  4 1.老男孩, 2.拼客科技, 3.返回, 4.退出1
  5 1.学员视图
  6 2.讲师视图
  7 3.学校管理视图
  8 4.返回
  9 5.退出
 10 请选择视图:1
 11 *********************欢迎进入学员视图*********************
 12 1.欢迎注册
 13 2.返回
 14 3.退出
 15 请选择:1
 16 请输入学生姓名:zcl
 17 请输入学生性别:m
 18 请输入学生年龄:22
 19 请输入学生序号22
 20 1.Linux[11800RMB], 2.Python[6400RMB], 3.CCIE[2400RMB], 4.返回
 21 请选择课程:1
 22 
 23         ---------------学生zcl的信息--------------
 24         Name:zcl
 25         School:老男孩
 26         Sex:m
 27         Age:22
 28         ID:22
 29         Course:Linux
 30         Course_price:11800
 31         
 32 1.欢迎注册
 33 2.返回
 34 3.退出
 35 请选择:2
 36 1.学员视图
 37 2.讲师视图
 38 3.学校管理视图
 39 4.返回
 40 5.退出
 41 请选择视图:3
 42 ********************欢迎进入学校管理视图********************
 43 1.创建班级
 44 2.创建课程
 45 3.雇用讲师
 46 4.返回
 47 请选择:1
 48 ----------------------欢迎创建班级----------------------
 49 请输入班级名称:s13
 50 请输入班级周期:1 year
 51 ------------------班级成功创建,班级信息如下-------------------
 52 班级名称:s13
 53 班级周期:1 year
 54 1.创建班级
 55 2.创建课程
 56 3.雇用讲师
 57 4.返回
 58 请选择:2
 59 ----------------------迎欢创建课程----------------------
 60 请输入课程类型[eg:技术/教育/自然科学/艺术...]:tenology
 61 请输入课程名称:py
 62 请输入课程价格:6500
 63 请输入课程周期:6 month
 64 ------------------课程成功创建,课程信息如下-------------------
 65 课程类型:tenology,名称:py,价格:6500,周期:6 month
 66 1.创建班级
 67 2.创建课程
 68 3.雇用讲师
 69 4.返回
 70 请选择:3
 71 ----------------------欢迎雇用讲师----------------------
 72 请输入讲师名字:alex
 73 请输入讲师性别:m
 74 请输入讲师年龄:33
 75 请输入讲师对应课程:lpy
 76 请输入讲师对应班级:s14
 77 
 78         -------------讲师alex的信息-------------
 79         Name:alex
 80         Sex:m
 81         Age:33
 82         Course:lpy
 83         Classroom:s14
 84         School_name:老男孩
 85         
 86 1.创建班级
 87 2.创建课程
 88 3.雇用讲师
 89 4.返回
 90 请选择:4
 91 1.学员视图
 92 2.讲师视图
 93 3.学校管理视图
 94 4.返回
 95 5.退出
 96 请选择视图:2
 97 *********************欢迎进入讲师视图*********************
 98 请输入讲师姓名:alex
 99 ----------------------欢迎alex讲师----------------------
100 1.查看班级
101 2.查看学员信息
102 3.返回
103 4.退出
104 请选择:1
105 功能未完善,只能输入Alex,cheng
106 ----------------------欢迎alex讲师----------------------
107 1.查看班级
108 2.查看学员信息
109 3.返回
110 4.退出
111 请选择:3
112 1.学员视图
113 2.讲师视图
114 3.学校管理视图
115 4.返回
116 5.退出
117 请选择视图:2
118 *********************欢迎进入讲师视图*********************
119 请输入讲师姓名:Alex
120 ----------------------欢迎Alex讲师----------------------
121 1.查看班级
122 2.查看学员信息
123 3.返回
124 4.退出
125 请选择:1
126 班级名称:S13
127 班级周期:7 month
128 ----------------------欢迎Alex讲师----------------------
129 1.查看班级
130 2.查看学员信息
131 3.返回
132 4.退出
133 请选择:2
134 请输入要查看学生名字:zcl
135 
136         ---------------学生zcl的信息--------------
137         Name:zcl
138         School:老男孩
139         Sex:m
140         Age:22
141         ID:22
142         Course:Linux
143         Course_price:11800
144         
145 ----------------------欢迎Alex讲师----------------------
146 1.查看班级
147 2.查看学员信息
148 3.返回
149 4.退出
150 请选择:2
151 请输入要查看学生名字:zcl
152 
153         ---------------学生zcl的信息--------------
154         Name:zcl
155         School:老男孩
156         Sex:m
157         Age:22
158         ID:22
159         Course:Linux
160         Course_price:11800
161         
162 ----------------------欢迎Alex讲师----------------------
163 1.查看班级
164 2.查看学员信息
165 3.返回
166 4.退出
167 请选择:3
168 1.学员视图
169 2.讲师视图
170 3.学校管理视图
171 4.返回
172 5.退出
173 请选择视图:
174 Process finished with exit code 1
View Code

 

python之选课系统详解[功能未完善]