python中常见的运行时错误_常见Python运行时错误之一
1)忘記在 if , elif , else , for , while , class ,def 聲明末尾添加 :(導(dǎo)致 “SyntaxError :invalid syntax”)
2)使用 = 而不是 ==(導(dǎo)致“SyntaxError: invalid syntax”) = 是賦值操作符而 == 是等于比較操作。
3)錯(cuò)誤的使用縮進(jìn)量。(導(dǎo)致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)
記住縮進(jìn)增加只用在以:結(jié)束的語句之后,而之后必須恢復(fù)到之前的縮進(jìn)格式。
4)在 for 循環(huán)語句中忘記調(diào)用 len() (導(dǎo)致“TypeError: ‘list’ object cannot be interpreted as an integer”)
通常你想要通過索引來迭代一個(gè)list或者string的元素,這需要調(diào)用 range() 函數(shù)。要記得返回len 值而不是返回這個(gè)列表。
5)嘗試修改string的值(導(dǎo)致“TypeError: ‘str’ object does not support item assignment”)
string是一種不可變的數(shù)據(jù)類型
6)嘗試連接非字符串值與字符串(導(dǎo)致 “TypeError: Can’t convert ‘int’ object to str implicitly”)
7)在字符串首尾忘記加引號(hào)(導(dǎo)致“SyntaxError: EOL while scanning string literal”)
8)變量或者函數(shù)名拼寫錯(cuò)誤(導(dǎo)致“NameError: name ‘fooba’ is not defined”)
9)方法名拼寫錯(cuò)誤(導(dǎo)致 “AttributeError: ‘str’ object has no attribute ‘lowerr’”)
10)引用超過list最大索引(導(dǎo)致“IndexError: list index out of range”)
總結(jié)
以上是生活随笔為你收集整理的python中常见的运行时错误_常见Python运行时错误之一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 睡眠模式下如何保持下载如何在休眠状态下下
- 下一篇: python数据降维_使用python实