当前位置:   article > 正文

Python常见报错_indexerror: tuple index out of range

indexerror: tuple index out of range

IndexError:索引越界异常

1.IndexError: tuple index out of range
错误的原因是元组索引越界,因为后面format中的内容出现了赋值操作
在这里插入图片描述

TypeError:类型错误

1.TypeError: ‘int’ object has no attribute 'getitem在这里插入图片描述
会出现报错:
在这里插入图片描述

在python2中,input输入数字得到的就是int类型的数字,数字是不可以做下标取值,而python3中input输入都为字符串,而字符串可以下标索引

2.TypeError: unbound method say_hi() must be called with Person instance as first argument (got nothing instead)
原因:调用类报错,由于未添加括号情况下,未被认为是类的实例,故报此错
解决方法:在类名称后边加括号()
在这里插入图片描述
在这里插入图片描述
3.TypeError: ‘int’ object is not callable
原因:int对象不可调用,当变量名和函数名重复时,程序会默认调用Int型对象,Int对象没有什么调用可言,因此报错
解决方法:更改变量名或者更改函数名
在这里插入图片描述
在这里插入图片描述
4.TypeError: init() takes exactly 3 arguments (5 given)
原因:传输的参数不一致
在实现继承时,继承父类的方法,构造函数中也需要所用到的父类的参数。构造函数中包含调用父类构造函数。根据需要,不是必须。 子类并不会自动调用父类的__init__(),我们必须显式的调用它。
在这里插入图片描述
报错:
在这里插入图片描述
修改后运行正常:
在这里插入图片描述

SyntaxError:语法错误

1.SyntaxError: ‘break’ outside loop
原因:break只能在for循环和while循环中使用
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/861456
推荐阅读
相关标签
  

闽ICP备14008679号