IndexError: tuple index out of range 您所在的位置:网站首页 华数错误代码405是什么意思 IndexError: tuple index out of range

IndexError: tuple index out of range

2024-01-06 03:06| 来源: 网络整理| 查看: 265

class Vector2d: typecode = 'd'#'d' float 8 typecode 是类属性 在 Vector2d 实例和字节序列之间转换时使用 def __init__(self,x,y): #把 x 和 y 转换成浮点数,尽早捕获错误,以防调用 Vector2d 函数时传入不当参数 self.x = float(x) self.y = float(y) def __iter__(self):#把 Vector2d 实例变成可迭代的对象 return (i for i in (self.x,self.y))#返回一个列表 这里i=[x,y] def __repr__(self): class_name = type(self).__name__ #使用 {!r} 获取各个分量的表示形式,然后插值 return '{}({!r},{!r})'.format(class_name,*self) #错误写法: #(1):return '{}({!r},{!r})'.format((class_name, *self)) #(2)return '{}({!r},{!r})'.format(class_name)

python调试报错为:IndexError: tuple index out of range(元组指数超出范围 )

原因:

1)可能其中一个索引是错误的,无论是内部索引还是外部索引。比如错误写法(1)

2)出现遗漏现象,比如两个{!r}只匹配了其中一个。如错误写法(2)

 

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有