作業需求
思路: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()
輸出測試:

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