这段代码是错误在哪里呢?

这段代码是错误在哪里呢?

#定义的教师类模块

class Teather(object):
   ''' 教师类 '''
   def __init__(self, t_name, t_number, phone):
       '''
       教师的基本信息
       :param name: 教师姓名
       :param t_number: 教师编号
       :param phone: 教师的电话号码
       '''
       self.t_name = t_name
       self.t_number = t_number
       self.phone = phone

   def __str__(self):
       return 'name:{},t_number:{}'.format(self.t_name, self.t_number)

#教师信息初始化及运用

import Teather

def create_teacher():
   ''' 教师信息初始化 '''
   dict_teacher = {"T1":["T1", "张亮", "13301122001"], "T2":["T2", "王朋", "13301122002"],
                   "T3":["T3", "李旭", "13301122003"], "T4":["T4", "黄国发", "13301122004"],
                   "T5":["T5", "周勤", "13301122005"], "T6":["T6", "谢富顺", "13301122006"],
                   "T7":["T7", "贾教师", "13301122007"], "T8":["T8", "杨教师", "13301122008"]}
   list_teacher = []
   for k in dict_teacher:
       t = Teather(*dict_teacher[k])
       print(t)
       list_teacher.append(t)
       print(list_teacher)
   return list_teacher


#运行结果:

if __name__ == '__main__':
   # list_ct = course_to_teacher()
   # print(list_ct)
   # list_s = create_student()
   l = create_teacher()

name:T1,t_number:张亮

[<homework.project_homework.example.Teather object at 0x007E9050>]

name:T2,t_number:王朋

[<homework.project_homework.example.Teather object at 0x007E9050>, <homework.project_homework.example.Teather object at 0x00823650>]

name:T3,t_number:李旭

[<homework.project_homework.example.Teather object at 0x007E9050>, <homework.project_homework.example.Teather object at 0x00823650>, <homework.project_homework.example.Teather object at 0x008238F0>]

name:T4,t_number:黄国发

[<homework.project_homework.example.Teather object at 0x007E9050>, <homework.project_homework.example.Teather object at 0x00823650>, <homework.project_homework.example.Teather object at 0x008238F0>, <homework.project_homework.example.Teather object at 0x00823950>]

name:T5,t_number:周勤

[<homework.project_homework.example.Teather object at 0x007E9050>, <homework.project_homework.example.Teather object at 0x00823650>, <homework.project_homework.example.Teather object at 0x008238F0>, <homework.project_homework.example.Teather object at 0x00823950>, <homework.project_homework.example.Teather object at 0x00823970>]

name:T6,t_number:谢富顺

[<homework.project_homework.example.Teather object at 0x007E9050>, <homework.project_homework.example.Teather object at 0x00823650>, <homework.project_homework.example.Teather object at 0x008238F0>, <homework.project_homework.example.Teather object at 0x00823950>, <homework.project_homework.example.Teather object at 0x00823970>, <homework.project_homework.example.Teather object at 0x008239D0>]

name:T7,t_number:贾教师

[<homework.project_homework.example.Teather object at 0x007E9050>, <homework.project_homework.example.Teather object at 0x00823650>, <homework.project_homework.example.Teather object at 0x008238F0>, <homework.project_homework.example.Teather object at 0x00823950>, <homework.project_homework.example.Teather object at 0x00823970>, <homework.project_homework.example.Teather object at 0x008239D0>, <homework.project_homework.example.Teather object at 0x00823990>]

name:T8,t_number:杨教师

[<homework.project_homework.example.Teather object at 0x007E9050>, <homework.project_homework.example.Teather object at 0x00823650>, <homework.project_homework.example.Teather object at 0x008238F0>, <homework.project_homework.example.Teather object at 0x00823950>, <homework.project_homework.example.Teather object at 0x00823970>, <homework.project_homework.example.Teather object at 0x008239D0>, <homework.project_homework.example.Teather object at 0x00823990>, <homework.project_homework.example.Teather object at 0x008239F0>]

为什么得不到相关信息的列表呢?是哪里出错了,老师帮忙看看

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手乔木 2019-12-19 17:52:10

同学你好:

同学的代码没有问题。

向列表中添加的是每一个Teacher的实例对象。

http://img1.sycdn.imooc.com//climg/5dfb476909f32ef305210592.jpg

2、

同学需要注意编号好姓名的对应位置。

http://img1.sycdn.imooc.com//climg/5dfb47d909af8e0302650050.jpg

同学需要修改构造函数的参数列表的参数顺序。

修改后:

http://img1.sycdn.imooc.com//climg/5dfb4847090b9a3704260047.jpg

如果我解决了同学的问题,请采纳!学习愉快^_^。

  • def course_to_teacher(): ''' 实现课程与老师逐一绑定 ''' list_ct = [] #课程信息 ls_course = prepare_course() #教师信息 ls_teacher = create_teacher() for i in range(0, len(ls_course)): ''' 循环绑定课程和老师 ''' ct = ls_course[i].binding(ls_teacher[-(i+1)][2]) #可是系统报了这一行出错,是什么原因呢? list_ct.append(ct) return list_ct if __name__ == '__main__': list_ct = course_to_teacher() print(list_ct) list_s = create_student() Traceback (most recent call last): File "G:/python-project/homework/project_homework/fiel_exe.py", line 62, in <module> list_ct = course_to_teacher() File "G:/python-project/homework/project_homework/fiel_exe.py", line 46, in course_to_teacher ct = ls_course[i].binding(ls_teacher[-(i+1)][2]) TypeError: 'Teather' object is not subscriptable 老师,这是报错是什么原因呢?我没看懂要怎么修改,老师帮看一下
    2019-12-19 18:04:19
  • def course_to_teacher(): ''' 实现课程与老师逐一绑定 ''' list_ct = [] #课程信息 ls_course = prepare_course() #教师信息 ls_teacher = create_teacher() for i in range(0, len(ls_course)): ''' 循环绑定课程和老师 ''' ct = ls_course[i].binding(ls_teacher[-(i+1)][2]) #可是系统报了这一行出错,是什么原因呢? list_ct.append(ct) return list_ct if __name__ == '__main__': list_ct = course_to_teacher() print(list_ct) list_s = create_student() Traceback (most recent call last): File "G:/python-project/homework/project_homework/fiel_exe.py", line 62, in <module> list_ct = course_to_teacher() File "G:/python-project/homework/project_homework/fiel_exe.py", line 46, in course_to_teacher ct = ls_course[i].binding(ls_teacher[-(i+1)][2]) TypeError: 'Teather' object is not subscriptable 老师,这是报错是什么原因呢?我没看懂要怎么修改,老师帮看一下
    2019-12-19 18:06:10
  • 同学你好: 1、同学提供的代码不全,无法判断同学的具体错误。2、同学在回复中的代码是没有格式的,所以建议同学不要在回复中将代码上传。3.根据同学的这部分代码和报错信息。ls_teacher[-(i+1)][2]中ls_teacher[-(i+1)]得到的是一个teacher对象,不是list类型,所以无法再进行截取。直接ls_teacher[-(i+1)]即可。4、同学的问题如果没有解决,建议同学重新在问答区提问并上传相关的代码。学习愉快^_^。
    2019-12-19 18:18:59
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
1.Python零基础入门
  • 参与学习           人
  • 提交作业       2727    份
  • 解答问题       8160    个

想要进入Python Web、爬虫、人工智能等高薪领域,你需要掌握本阶段的Python基础知识,课程安排带你高效学习轻松入门,学完你也能听得懂Python工程师的行业梗。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师