完成一个作业:
1. 创建北京、上海 2 所学校
2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开
3. 课程包含,周期,价格
4. 班级关联课程、讲师
5. 创建学员时,选择学校,关联班级
5. 创建讲师角色时要关联学校
6. 提供三个角色视图
6.1 学员视图, 登陆, 查看课程、查看班级
6.2 讲师视图, 讲师可查看自己教学的班级、课程。
进阶需求:可管理自己的班级, 查看班级学员列表 , 修改所管理的学员的成绩
6.3 管理视图,创建讲师, 创建班级,创建课程
7. 上面的操作产生的数据都通过pickle序列化保存到文件里
先上传一个版本,其中讲师部分的功能还未完成。
import pickle
import sys
# 学校类
class School:
def __init__(self, school_name, school_address):
self.school_name = school_name
self.school_address = school_address
self.class_list = [] # 学校类中应该有一个班级列表
# 课程类
class Course:
def __init__(self, name, period, price):
self.name = name
self.period = period
self.price = price
def course_info(self):
print('''
---------------课程信息--------------
Name: %s
period: %s
price: %s
------------------------------------
''' % (self.name, self.period, self.price))
# 讲师类
class Lecturer:
def __init__(self, name, age):
self.name = name
self.age = age
def lecturer_info(self):
print('''
---------------讲师信息--------------
Name: %s
Age: %s
------------------------------------
''' % (self.name, self.age))
# 班级类
# 预先要创建课程和讲师, 然后再做关联
class Class:
def __init__(self, class_id, course_name, lecturer_name):
self.class_id = class_id
self.course_name = course_name
self.lecturer_name = lecturer_name
def class_info(self):
print('''
---------------班级信息--------------
ClassID: %s
CourseName: %s
LecturerName: %s
------------------------------------
''' % (self.class_id, self.course_name, self.lecturer_name))
# 学员类
class Student:
def __init__(self, name, school_name, class_name):
self.name = name
self.school_name = school_name
self.class_name = class_name
def student_info(self):
print('''
---------------学生信息--------------
Name: %s
School: %s
Class: %s
------------------------------------
''' % (self.name, self.age, self.class_name))
# 查看讲师
def view_lecturers():
with open('lecturers_db', 'rb') as f:
res = pickle.load(f)
print('----------讲师列表-----------')
if res:
count = 1
for k in res:
print('''
-------------------------
No: %d
LecturerName: %s
LecturerAge: %s
-------------------------
''' % (count, res[k]['LecturerName'], res[k]['LecturerAge']))
count += 1
else:
print('讲师还未创建')
# 查看课程
def view_courses():
with open('courses_db', 'rb') as f:
res = pickle.load(f)
print('----------课程列表------------')
if res:
count = 1
for k in res:
print('''
--------- ---------------
No: %d
Name: %s
Period: %s
Price: %s
-------------------------
''' % (count, res[k]['CourseName'], res[k]['CoursePeriod'], res[k]['CoursePrice']))
count += 1
else:
print('课程还未创建')
# 查看班级
def view_classes():
print("查看班级")
with open('classes_db', 'rb') as f:
res = pickle.load(f)
print('----------班级列表------------')
if res:
count = 1
for k in res:
print('''
--------- ---------------
No: %d
ClassID: %s
ClassCourse: %s
ClassLecturer: %s
-------------------------
''' % (count, res[k]['ClassID'], res[k]['ClassCourse'], res[k]['ClassLecturer']))
count += 1
else:
print('班级还未创建')
# 创建学生视图:
def student_view():
while True:
prompt = '''
1. 查看讲师
2. 查看课程
3. 查看班级
4. 返回
5. 退出
'''
num = input(prompt)
if num == '1':
view_lecturers()
elif num == '2':
view_courses()
elif num == '3':
view_classes()
elif num == '4':
break
elif num == '5':
sys.exit()
else:
print('你输出错误,请重新输入!')
continue
# 讲师查看自己教学的班级
def view_self_classes():
pass
# 讲师查看自己教学的课程
def view_self_courses():
pass
# 讲师查看班级学员列表
def view_class_student_list():
pass
# 讲师修改学生成绩
def modify_student_score():
pass
def lecturers_view():
while True:
prompt = '''
1. 查看自己教学的班级
2. 查看自己教学的课程
3. 查看班级学员列表
4. 修改管理员的成绩
'''
num = input(prompt)
if num == '1':
view_self_classes()
elif num == '2':
view_self_courses()
elif num == '3':
view_class_student_list()
elif num == '4':
modify_student_score()
else:
print('你输出错误,请重新输入!')
continue
# 定义管理员视图
def admin_view():
while True:
prompt = '''
1. 创建讲师
2. 创建课程
3. 创建班级
4. 返回
5. 退出
'''
num = input(prompt)
if num == '1':
add_lecturer()
elif num == '2':
add_course()
elif num == '3':
add_class()
elif num == '4':
break
elif num == '5':
sys.exit()
else:
print('你输出错误,请重新输入!')
continue
# 添加讲师函数
# 讲师是关联学校
def add_lecturer():
print("添加讲师函数")
# 输入讲师的相关信息
new_lecturer_name = input("请输入姓名: ")
new_lecturer_age = input("请输入年龄: ")
# 创建了Lecturer对象
new_lecturer = Lecturer(new_lecturer_name, new_lecturer_age)
# print("new_lecturer的信息:", new_lecturer.name, new_lecturer.age)
new_lecturer_dict = {'LecturerName': new_lecturer_name, 'LecturerAge': new_lecturer_age}
if not dic:
# 将这个用户字典添加到全局字典中,由用户名:用户字典组成
lecturers_dict[new_lecturer_name] = new_lecturer_dict
with open('lecturers_db', 'wb') as f:
pickle.dump(lecturers_dict, f)
else:
if dic.get(new_lecturer_name):
print('%s 讲师已存在,不能重名' % new_lecturer_name)
else:
lecturers_dict[new_lecturer_name] = new_lecturer_dict
with open('lecturers_db', 'wb') as f:
pickle.dump(lecturers_dict, f)
new_lecturer.lecturer_info()
# 创建课程
#
def add_course():
print("创建课程函数")
# 输入课程的相关信息
new_course_name = input("请输入课程名称: ")
new_course_period = input("请输入课程周期: ")
new_course_price = input("请输入课程价格: ")
# 创建这个新课程的对象
new_course = Course(new_course_name, new_course_period, new_course_price)
# 生成一个字典
new_course_dict = {'CourseName': new_course_name, 'CoursePeriod': new_course_period,
'CoursePrice': new_course_price}
if not dic:
courses_dict[new_course_name] = new_course_dict
with open('courses_db', 'wb') as f:
pickle.dump(courses_dict, f)
else:
if dic.get(new_course_name):
print("%s 课程名字已存在,不能重名" % new_course_name)
else:
courses_dict[new_course_name] = new_course_dict
with open('courses_db', 'wb') as f:
pickle.dump(courses_dict, f)
new_course.course_info()
# 创建班级函数
# 班级要关联课程和讲师
def add_class():
print("添加班级函数")
new_class_id = input("请输入班级编号: ")
associate_class_course = input("请输入班级课程: ") # 输入关联班级课程
# 课程要course_db的课程进行关联
# course_db无此课程,则报错,提示无此课程
# course_db有这个课程,才能关联
with open('courses_db', 'rb') as f:
res = pickle.load(f)
if res:
if res.get(associate_class_course):
print('有此课程,关联成功')
else:
print('无此课程,请先创建此课程')
sys.exit()
associate_class_lecture = input("请输入班级讲师: ") # 输入关联班级讲师
# 讲师要与lecturer_db中的讲师进行关联
# lecture_db无此讲师, 则报错,提示无此讲师
# lecture_db有次讲师,才能关联
with open('lecturers_db', 'rb') as f:
res = pickle.load(f)
if res:
if res.get(associate_class_lecture):
print('有此讲师,关联成功1')
else:
print('无此讲师,请先创建此讲师')
sys.exit
new_class = Class(new_class_id, associate_class_course, associate_class_lecture)
# 生成一个字典
new_class_dict = {'ClassID': new_class_id, 'ClassCourse': associate_class_course,
'ClassLecturer': associate_class_lecture}
if not dic: # 如果字典为空
classes_dict[new_class_id] = new_class_dict # 将班级编号和班级对象映射
with open('classes_db', 'wb') as f:
pickle.dump(classes_dict, f) # 将班级的字典序列化到class_db中
else:
if dic.get(new_class_id): # else字典不为空,且获得班级号,说明班级号已存在
print("%s 班级号已存在,不能重号" % new_class_id)
else: # else字典不为空,没有班级号
classes_dict[new_class_id] = new_class_dict
with open('classes_db', 'wb') as f:
pickle.dump(classes_dict, f)
new_class.class_info()
def main():
while True:
prompt = '''
1. 学员视图
2. 讲师视图
3. 管理视图
'''
num = input(prompt)
if num == '1':
student_view()
elif num == '2':
lecturers_view()
elif num == '3':
admin_view()
else:
print('你输出错误,请重新输入!')
continue
if __name__ == '__main__':
# 声明为全局变量
global dic
# 定义一个空的讲师字典
lecturers_dict = {}
# 定义一个空的课程字典
courses_dict = {}
# 定义一个空的班级字典
classes_dict = {}
# 全局字典dic
dic = {}
# 直接实例化两个学校
beijing_school = School('北京校区', '北京王府井')
shanghai_school = School('上海校区', '上海徐家汇')
# 打印下两个学校的名称
print(beijing_school.school_name, shanghai_school.school_name)
main()