萬事開頭難!Python 初學(xué)者最容易犯的幾個錯誤

對于新手,初學(xué)Python時,總會遇到這樣那樣的報錯,想要弄懂Python錯誤信息的含義可能還不知道怎么做,這里列出了一些比較常見的Python報錯問題,希望對于學(xué)習(xí)Python的人能夠有些幫助。

1)嘗試連接非字符串值與字符串(導(dǎo)致 “TypeError: Can’t convert ‘int’ object to str implicitly”)

該錯誤發(fā)生在如下代碼中:

numEggs = 12print('I have ' + numEggs + ' eggs.')

而你實(shí)際想要這樣做:

numEggs = 12print('I have ' + str(numEggs) + ' eggs.')

或者:

numEggs = 12print('I have %s eggs.' % (numEggs))

2)在字符串首尾忘記加引號(導(dǎo)致“SyntaxError: EOL while scanning string literal”)

該錯誤發(fā)生在如下代碼中:

print(Hello!')

或者:

print('Hello!)

或者:

myName = 'Al'print('My name is ' + myName + . How are you?')

image.png

3)變量或者函數(shù)名拼寫錯誤(導(dǎo)致“NameError: name ‘fooba’ is not defined”)

該錯誤發(fā)生在如下代碼中:

foobar = 'Al'print('My name is ' + fooba)

或者:

spam = ruond(4.2)

或者:

spam = Round(4.2)

4)方法名拼寫錯誤(導(dǎo)致 “AttributeError: ‘str’ object has no attribute ‘lowerr‘”)

該錯誤發(fā)生在如下代碼中:

spam = 'THIS IS IN LOWERCASE.'spam = spam.lowerr()

image.png

5)引用超過list最大索引(導(dǎo)致“IndexError: list index out of range”)

該錯誤發(fā)生在如下代碼中:

spam = ['cat', 'dog', 'mouse']
print(spam[6])

6)使用不存在的字典鍵值(導(dǎo)致“KeyError:‘spam’”)

該錯誤發(fā)生在如下代碼中:

spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'}
print('The name of my pet zebra is ' + spam['zebra'])

image.png

7)忘記在 if, elif , else , for , while , class ,def 聲明末尾添加 :(導(dǎo)致 “SyntaxError :invalid syntax”)

該錯誤將發(fā)生在類似如下代碼中:

if spam == 42
print('Hello!')

8)使用 = 而不是 ==(導(dǎo)致“SyntaxError: invalid syntax”)

= 是賦值操作符而 == 是等于比較操作。該錯誤發(fā)生在如下代碼中:

if spam = 42:
print('Hello!')

image.png

9)使用錯誤的縮進(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)格式。該錯誤發(fā)生在如下代碼中:

print('Hello!')
print('Howdy!')

或者:if spam == 42:

print('Hello!')

print('Howdy!')

或者:

if spam == 42:
print('Hello!')

image.png

10)在 for循環(huán)語句中忘記調(diào)用 len()(導(dǎo)致“TypeError: ‘list’ object cannot be interpreted as an integer”)

通常你想要通過索引來迭代一個list或者string的元素,這需要調(diào)用 range() 函數(shù)。要記得返回len 值而不是返回這個列表。

該錯誤發(fā)生在如下代碼中:

spam = ['cat', 'dog', 'mouse']for i in range(spam):
print(spam[i])

python一些最重要的內(nèi)建異常類名總結(jié)

AttributeError:屬性錯誤,特性引用和賦值失敗時會引發(fā)屬性錯誤

NameError:試圖訪問的變量名不存在

SyntaxError:語法錯誤,代碼形式錯誤

Exception:所有異常的基類,因?yàn)樗衟ython異常類都是基類Exception的其中一員,異常都是從基類Exception繼承的,并且都在exceptions模塊中定義。
IOError:一般常見于打開不存在文件時會引發(fā)IOError錯誤,也可以解理為輸出輸入錯誤

KeyError:使用了映射中不存在的關(guān)鍵字(鍵)時引發(fā)的關(guān)鍵字錯誤

IndexError:索引錯誤,使用的索引不存在,常索引超出序列范圍,什么是索引

TypeError:類型錯誤,內(nèi)建操作或是函數(shù)應(yīng)于在了錯誤類型的對象時會引發(fā)類型錯誤

ZeroDivisonError:除數(shù)為0,在用除法操作時,第二個參數(shù)為0時引發(fā)了該錯誤

ValueError:值錯誤,傳給對象的參數(shù)類型不正確,像是給int()函數(shù)傳入了字符串?dāng)?shù)據(jù)類型的參數(shù)。

希望上邊的總結(jié),對大家能夠有一些幫助,如有不到之處,也希望多多指教,相互交流進(jìn)步。

小編是一名python開發(fā)工程師,這里有我自己整理了一套最新的python系統(tǒng)學(xué)習(xí)教程,包括從基礎(chǔ)的python腳本到web開發(fā)、爬蟲、數(shù)據(jù)分析、數(shù)據(jù)可視化、機(jī)器學(xué)習(xí)等。想要這些資料的可以關(guān)注小編。且我這有個學(xué)習(xí)Python基地,里面有很多學(xué)習(xí)資料,感興趣的+Q群:895817687

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容