300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python中常见的运行时错误_常见Python运行时错误之一

python中常见的运行时错误_常见Python运行时错误之一

时间:2019-05-20 03:21:12

相关推荐

python中常见的运行时错误_常见Python运行时错误之一

1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”)

2)使用 = 而不是 ==(导致“SyntaxError: invalid syntax”) = 是赋值操作符而 == 是等于比较操作。

3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)

记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。

4)在 for 循环语句中忘记调用 len() (导致“TypeError: ‘list’ object cannot be interpreted as an integer”)

通常你想要通过索引来迭代一个list或者string的元素,这需要调用 range() 函数。要记得返回len 值而不是返回这个列表。

5)尝试修改string的值(导致“TypeError: ‘str’ object does not support item assignment”)

string是一种不可变的数据类型

6)尝试连接非字符串值与字符串(导致 “TypeError: Can’t convert ‘int’ object to str implicitly”)

7)在字符串首尾忘记加引号(导致“SyntaxError: EOL while scanning string literal”)

8)变量或者函数名拼写错误(导致“NameError: name ‘fooba’ is not defined”)

9)方法名拼写错误(导致 “AttributeError: ‘str’ object has no attribute ‘lowerr’”)

10)引用超过list最大索引(导致“IndexError: list index out of range”)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。