欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

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

发布时间:2024/9/19 python 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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”)

总结

以上是生活随笔为你收集整理的python中常见的运行时错误_常见Python运行时错误之一的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。