不明白为何结果返回的是空列表
#类的定义example.py
class Student(object):
"""学生类"""
def __init__(self, s_name, s_number):
self.s_name = s_name
self.s_number = s_number
self.s_course = []
@property
def course_detail(self):
return self.s_course
def add_course(self, cour_info):
self.s_course.append(cour_info)
def __str__(self):
return 'name: {p1}, s_number: {p2}'.format(p1 = self.s_name, p2 = self.s_number)
class Teacher(object):
"""教师类"""
def __init__(self, t_number, t_name, t_phone):
self.t_number = t_number
self.t_name = t_name
self.t_phone = t_phone
def __str__(self):
return 't_number: {p1}, t_name: {p2}'.format(p1 = self.t_number, p2 = self.t_name)
class Course(object):
"""课程类"""
def __init__(self, c_number, c_name, c_teacher = None):
self.c_number = c_number
self.c_name = c_name
self.c_teacher = c_teacher
def binding(self, teacher):
if isinstance(teacher, Teacher):
self.c_teacher = teacher
dict = {}
return dict.update(课程名称 = self.c_name, 教师名称 = self.c_teacher)
else:
return None
#完成业务逻辑file_exe.py
from example import Student, Teacher, Course
def introduction(str):
print('********************{0}********************'.format(str))
def prepare_course():
course_dict = {"01": "网络爬虫", "02": "数据分析", "03": "人工智能", "04": "机器学习", "05": "云计算", "06": "大数据", "07": "图像识别", "08": "Web开发"}
course_list = []
for key, value in course_dict:
course_mooc = Course(key, value)
course_list.append(course_mooc)
return course_list
def creat_teacher():
all_teacher_info = [["T1", "张亮", "13301122001"], ["T2", "王朋", "13301122002"], ["T3", "李旭", "13301122003"], ["T4", "黄国发", "13301122004"], ["T5", "周勤", "13301122005"], ["T6", "谢富顺", "13301122006"], ["T7", "贾教师", "13301122007"], ["T8", "杨教师", "13301122008"]]
teacher_list = []
for i in range(0, len(all_teacher_info)):
teacher_info = all_teacher_info[i]
teacher_instance = Teacher(teacher_info[0], teacher_info[1], teacher_info[2])
teacher_list.append(teacher_instance)
return teacher_list
def course_to_teacher():
course_to_teacher_list = []
ls_course = prepare_course()
ls_teacher = creat_teacher()
ls_teacher.reverse()
for i in range(0, len(ls_course)):
course_data = ls_course[i]
course_to_teacher_data = course_data.binding(ls_teacher[i])
course_to_teacher_list.append(course_to_teacher_data)
return course_to_teacher_list
def creat_student():
ls_student = ["小亮", "小明", "李红", "小丽", "Jone", "小彤", "小K", "慕慕"]
ls_s_number = []
for i in range(1000, 1008):
ls_s_number.append(i)
ls_student.reverse()
number_to_student_list = []
for p in range(0, len(ls_s_number)):
number_to_student_data = Student(ls_student[p], ls_s_number[p])
number_to_student_list.append(number_to_student_data)
return number_to_student_list
if __name__ == '__main__':
course_to_teacher()
creat_student()
introduction('慕课学院(1)班学生信息')
for i in creat_student():
print(i)
introduction('慕课学院(1)班选课结果')
for p in range(0, len(course_to_teacher())):
creat_student()[p].add_course(course_to_teacher()[p])
#print(creat_student()[p].course_detail)
for x in creat_student():
result = 'name: {p1}, Selected: {p2}'.format(p1 = x.s_name, p2 = x.course_detail)
print(result)
正在回答 回答被采纳积分+1
同学,你好。程序中有两点问题:
字典的update方法没有任何返回值,直接更新原有对象,这导致了输出为空列表,还有一点要注意,绑定的教师名称应是self.c_teacher.t_name,如果写成self.c_teacher则是绑定的一个教师对象,同学可进行如下修改:
同学的代码中,在给学生添加课程数据时,每次循环都运行了creat_student()函数,则之前添加的课程信息都没有保存,最终在输出学生信息时,也运行了一遍creat_student(),那么所创建的对象也是新对象,最终输出的学生课程信息是None,修改如下图:
如果解决了你的疑惑,请采纳,祝学习愉快~
- 参与学习 人
- 提交作业 2727 份
- 解答问题 8160 个
想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星