为什么教师名称输出不对
# 描述文件example.py
class Student(object):
def __init__(self, s_number, name, course = None):
"""
描述学生的学号、姓名、已选课程
:param s_number: 学生学号
:param name: 学生姓名
:param course: 已选课程(默认值为空列表)
"""
self.s_number = s_number
self.name = name
self.course = course
def course_detail(self):
return self.course
def add_course(self,cour_info):
self.course = cour_info
def __str__(self):
return 'name:{0},s_number:{1}'.format(self.name, self.s_number)
class Teacher(object):
def __init__(self, t_number, t_name, phone_number):
"""
描述教师编号、教师姓名、手机号码
:param t_number:教师编号
:param t_name:教师姓名
:param phone_number:手机号码
"""
self.t_number = t_number
self.t_name = t_name
self.phone_number = phone_number
def __str__(self):
return self.t_number, self.t_name
class Course(object):
def __init__(self, course_number, course_name, teacher = None):
"""
描述课程编号、课程名称、授课教师
:param course_number:课程编号
:param course_name:课程名称
:param teacher:授课教师
"""
self.course_number = course_number
self.course_name = course_name
self.teacher = teacher
def binding(self, teacher):
if not teacher:
return
self.teacher = teacher
course_dict = {'课程名称': self.course_name, '教师名称': self.teacher}
return course_dict
# 启动文件file_exe.py
from example import Course, Teacher, Student
def introduction(str):
print('{0}{1}{2}'.format('**********', str, '**********'))
def prepare_course():
course_dit = {"01": "网络爬虫", "02": "数据分析", "03": "人工智能", "04": "机器学习",
"05": "云计算", "06": "大数据", "07": "图像识别", "08": "Web开发"}
course_list = []
for course_number, course_name in course_dit.items():
course = Course(course_number, course_name)
course_list.append(course)
return course_list
def create_teacher():
teacher_info_list = [["T1", "张亮", "13301122001"], ["T2", "王朋", "13301122002"],
["T3", "李旭", "13301122003"], ["T4", "黄国发", "13301122004"],
["T5", "周勤", "13301122005"], ["T6", "谢富顺", "13301122006"],
["T7", "贾教师", "13301122007"], ["T8", "杨教师", "13301122008"]
]
teacher_list = []
for i in teacher_info_list:
teacher_info = Teacher(i[0], i[1], i[2])
teacher_list.append(teacher_info)
return teacher_list
def course_to_teacher():
course_teacher_list = []
is_course = prepare_course()
is_teacher = create_teacher()
for i in range(len(is_course)):
bind = is_course[i].binding(is_teacher[-(i - 1)])
course_teacher_list.append(bind)
return course_teacher_list
def create_student():
is_student = ["小亮", "小明", "李红", "小丽", "乔治", "小彤", "小凯", "慕慕"]
student_number = range(1000, 1008)
student_list = []
for w in range(len(is_student)):
student_info = Student(student_number[w], is_student[-(w + 1)])
student_list.append(student_info)
return student_list
if __name__ == "__main__":
student_info_list = create_student()
choose_list = course_to_teacher()
introduction("慕课学院(1)班学生信息")
for student in student_info_list:
print(student)
introduction("慕课学院(1)班选课信息")
for i in range(len(choose_list)):
student_info_list[i].add_course(choose_list[i])
for i in student_info_list:
print("name: {0}, Selected: {1}".format(i.name, i.course_detail()))
正在回答
同学你好:
1 course 是列表,同学需要在初识化时将其定义为列表。
2 course_detail 是属性方法,需要使用property装饰器。
3 将课程信息添加到学生的course列表中。
4 将教师逆序和课程绑定直接-i-1即可,同学的方法,索引从1 , 0 ,-1的顺序。
5 course_detail是属性方法,调用时直接方法名即可。
如果我解决了同学的问题,请采纳!学习愉快^_^。
- 参与学习 人
- 提交作业 2727 份
- 解答问题 8160 个
想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星