python keyerror_盘点Python 初学者最容易犯的10大错误!你中招了吗?
對(duì)于新手,初學(xué)Python時(shí),總會(huì)遇到這樣那樣的報(bào)錯(cuò),想要弄懂Python錯(cuò)誤信息的含義可能還不知道怎么做,這里列出了一些比較常見的Python報(bào)錯(cuò)問題,希望對(duì)于學(xué)習(xí)Python的人能夠有些幫助。
發(fā)現(xiàn)有很多想要學(xué)習(xí)Python卻不知道如何下手的朋友,我這里整理了一些關(guān)于Python的學(xué)習(xí)資料,從基礎(chǔ)到入門到實(shí)戰(zhàn)都有!有需要的朋友可以關(guān)注并私信“01”免費(fèi)獲取...
1)嘗試連接非字符串值與字符串(導(dǎo)致 “TypeError: Can’t convert ‘int’ object to str implicitly”)
該錯(cuò)誤發(fā)生在如下代碼中:
numEggs = 12print('I have ' + numEggs + ' eggs.')而你實(shí)際想要這樣做:
numEggs = 12print('I have ' + str(numEggs) + ' eggs.')或者:
numEggs = 12print('I have %s eggs.' % (numEggs))2)在字符串首尾忘記加引號(hào)(導(dǎo)致“SyntaxError: EOL while scanning string literal”)
該錯(cuò)誤發(fā)生在如下代碼中:
print(Hello!')或者:
print('Hello!)或者:
myName = 'Al'print('My name is ' + myName + . How are you?')3)變量或者函數(shù)名拼寫錯(cuò)誤(導(dǎo)致“NameError: name ‘fooba’ is not defined”)
該錯(cuò)誤發(fā)生在如下代碼中:
foobar = 'Al'print('My name is ' + fooba)或者:
spam = ruond(4.2)或者:
spam = Round(4.2)4)方法名拼寫錯(cuò)誤(導(dǎo)致 “AttributeError: ‘str’ object has no attribute ‘lowerr‘”)
該錯(cuò)誤發(fā)生在如下代碼中:
spam = 'THIS IS IN LOWERCASE.'spam = spam.lowerr()5)引用超過list最大索引(導(dǎo)致“IndexError: list index out of range”)
該錯(cuò)誤發(fā)生在如下代碼中:
spam = ['cat', 'dog', 'mouse']print(spam[6])6)使用不存在的字典鍵值(導(dǎo)致“KeyError:‘spam’”)
該錯(cuò)誤發(fā)生在如下代碼中:
spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'}print('The name of my pet zebra is ' + spam['zebra'])7)忘記在 if, elif , else , for , while , class ,def 聲明末尾添加 :(導(dǎo)致 “SyntaxError :invalid syntax”)
該錯(cuò)誤將發(fā)生在類似如下代碼中:
if spam == 42print('Hello!')8)使用 = 而不是 ==(導(dǎo)致“SyntaxError: invalid syntax”)
= 是賦值操作符而 == 是等于比較操作。該錯(cuò)誤發(fā)生在如下代碼中:
if spam = 42:print('Hello!')9)使用錯(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é)束的語(yǔ)句之后,而之后必須恢復(fù)到之前的縮進(jìn)格式。該錯(cuò)誤發(fā)生在如下代碼中:
print('Hello!')print('Howdy!')或者:if spam == 42:
print('Hello!')
print('Howdy!')
或者:if spam == 42:
print('Hello!')
10)在 for循環(huán)語(yǔ)句中忘記調(diào)用 len()(導(dǎo)致“TypeError: ‘list’ object cannot be interpreted as an integer”)
通常你想要通過索引來(lái)迭代一個(gè)list或者string的元素,這需要調(diào)用 range() 函數(shù)。要記得返回len 值而不是返回這個(gè)列表。
該錯(cuò)誤發(fā)生在如下代碼中:
spam = ['cat', 'dog', 'mouse']for i in range(spam):print(spam[i])python一些最重要的內(nèi)建異常類名總結(jié)
AttributeError:屬性錯(cuò)誤,特性引用和賦值失敗時(shí)會(huì)引發(fā)屬性錯(cuò)誤
NameError:試圖訪問的變量名不存在
SyntaxError:語(yǔ)法錯(cuò)誤,代碼形式錯(cuò)誤
Exception:所有異常的基類,因?yàn)樗衟ython異常類都是基類Exception的其中一員,異常都是從基類Exception繼承的,并且都在exceptions模塊中定義。
IOError:一般常見于打開不存在文件時(shí)會(huì)引發(fā)IOError錯(cuò)誤,也可以解理為輸出輸入錯(cuò)誤
KeyError:使用了映射中不存在的關(guān)鍵字(鍵)時(shí)引發(fā)的關(guān)鍵字錯(cuò)誤
IndexError:索引錯(cuò)誤,使用的索引不存在,常索引超出序列范圍,什么是索引
TypeError:類型錯(cuò)誤,內(nèi)建操作或是函數(shù)應(yīng)于在了錯(cuò)誤類型的對(duì)象時(shí)會(huì)引發(fā)類型錯(cuò)誤
ZeroDivisonError:除數(shù)為0,在用除法操作時(shí),第二個(gè)參數(shù)為0時(shí)引發(fā)了該錯(cuò)誤
ValueError:值錯(cuò)誤,傳給對(duì)象的參數(shù)類型不正確,像是給int()函數(shù)傳入了字符串?dāng)?shù)據(jù)類型的參數(shù)。
希望上邊的總結(jié),對(duì)大家能夠有一些幫助,如有不到之處,也希望多多指教,相互交流進(jìn)步。
總結(jié)
以上是生活随笔為你收集整理的python keyerror_盘点Python 初学者最容易犯的10大错误!你中招了吗?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 朱光玉火锅是谁创立的?
- 下一篇: 淄博凭借烧烤出圈有哪些原因?