python出現這個錯誤是什麼原因

2021-03-04 01:51:22 字數 5706 閱讀 1404

1樓:小鏡子

蒐集了一些python最重要的內建異常類名,並做了簡單的介紹:

attributeerror:屬性錯誤,特性引用和賦值失敗時會引發屬性錯誤

nameerror:試圖訪問的變數名不存在

syntaxerror:語法錯誤,**形式錯誤

exception:所有異常的基類,因為所有python異常類都是基類exception的其中一員,異常都是從基類exception繼承的,並且都在exceptions模組中定義。

ioerror:一般常見於開啟不存在檔案時會引發ioerror錯誤,也可以解理為輸出輸入錯誤

keyerror:使用了對映中不存在的關鍵字(鍵)時引發的關鍵字錯誤

indexerror:索引錯誤,使用的索引不存在,常索引超出序列範圍,什麼是索引

typeerror:型別錯誤,內建操作或是函式應於在了錯誤型別的物件時會引發型別錯誤

zerodivisonerror:除數為0,在用除法操作時,第二個引數為0時引發了該錯誤

valueerror:值錯誤,傳給物件的引數型別不正確,像是給int()函式傳入了字串資料型別的引數

python是物件導向語言,所以程式丟擲的異常也是類

python 出現這個錯誤是什麼原因

2樓:小鏡子

蒐集了一些python最重要的內建異常類名,並做了簡單的介紹:

attributeerror:屬性錯誤,特性引用和賦值失敗時會引發屬性錯誤

nameerror:試圖訪問的變數名不存在

syntaxerror:語法錯誤,**形式錯誤

exception:所有異常的基類,因為所有python異常類都是基類exception的其中一員,異常都是從基類exception繼承的,並且都在exceptions模組中定義。

ioerror:一般常見於開啟不存在檔案時會引發ioerror錯誤,也可以解理為輸出輸入錯誤

keyerror:使用了對映中不存在的關鍵字(鍵)時引發的關鍵字錯誤

indexerror:索引錯誤,使用的索引不存在,常索引超出序列範圍,什麼是索引

typeerror:型別錯誤,內建操作或是函式應於在了錯誤型別的物件時會引發型別錯誤

zerodivisonerror:除數為0,在用除法操作時,第二個引數為0時引發了該錯誤

valueerror:值錯誤,傳給物件的引數型別不正確,像是給int()函式傳入了字串資料型別的引數

python是物件導向語言,所以程式丟擲的異常也是類

3樓:匿名使用者

要把錯誤的內容發出來才可以知道

以下是python常見錯誤

1)忘記在 if , elif , else , for , while , class ,def 宣告末尾新增 :(導致 「syntaxerror :invalid syntax」)

該錯誤將發生在類似如下**中:

if spam == 42

print('hello!')

2)使用 = 而不是 ==(導致「syntaxerror: invalid syntax」)

= 是賦值操作符而 == 是等於比較操作。該錯誤發生在如下**中:

if spam = 42:

print('hello!')

3)錯誤的使用縮排量。(導致「indentationerror:unexpected indent」、「indentationerror:

unindent does not match any outer indetation level」以及「indentationerror:expected an indented block」)

記住縮排增加只用在以:結束的語句之後,而之後必須恢復到之前的縮排格式。該錯誤發生在如下**中:

print('hello!')

print('howdy!')

或者:if spam == 42:

print('hello!')

print('howdy!')

或者:if spam == 42:

print('hello!')

4)在 for 迴圈語句中忘記呼叫 len() (導致「typeerror: 'list' object cannot be interpreted as an integer」)

通常你想要通過索引來迭代乙個list或者string的元素,這需要呼叫 range() 函式。要記得返回len 值而不是返回這個列表。

該錯誤發生在如下**中:

spam = ['cat', 'dog', 'mouse']

for i in range(spam):

print(spam[i])

5)嘗試修改string的值(導致「typeerror: 'str' object does not support item assignment」)

string是一種不可變的資料型別,該錯誤發生在如下**中:

spam = 'i have a pet cat.'

spam[13] = 'r'

print(spam)

而你實際想要這樣做:

spam = 'i have a pet cat.'

spam = spam[:13] + 'r' + spam[14:]

print(spam)

6)嘗試連線非字串值與字串(導致 「typeerror: can't convert 'int' object to str implicitly」)

該錯誤發生在如下**中:

numeggs = 12

print('i have ' + numeggs + ' eggs.')

而你實際想要這樣做:

numeggs = 12

print('i have ' + str(numeggs) + ' eggs.')

或者:numeggs = 12

print('i have %s eggs.' % (numeggs))

7)在字串首尾忘記加引號(導致「syntaxerror: eol while scanning string literal」)

該錯誤發生在如下**中:

print(hello!')

或者:print('hello!)

或者:myname = 'al'

print('my name is ' + myname + . how are you?')

8)變數或者函式名拼寫錯誤(導致「nameerror: name 'fooba' is not defined」)

該錯誤發生在如下**中:

foobar = 'al'

print('my name is ' + fooba)

或者:spam = ruond(4.2)

或者:spam = round(4.2)

9)方法名拼寫錯誤(導致 「attributeerror: 'str' object has no attribute 'lowerr'」)

該錯誤發生在如下**中:

spam = 'this is in lowercase.'

spam = spam.lowerr()

10)引用超過list最大索引(導致「indexerror: list index out of range」)

該錯誤發生在如下**中:

spam = ['cat', 'dog', 'mouse']

print(spam[6])

11)使用不存在的字典鍵值(導致「keyerror:『spam』」)

該錯誤發生在如下**中:

spam =

print('the name of my pet zebra is ' + spam['zebra'])

12)嘗試使用python關鍵字作為變數名(導致「syntaxerror:invalid syntax」)

python關鍵不能用作變數名,該錯誤發生在如下**中:

class = 'algebra'

python3

的關鍵字有:and, as, assert, break, class, continue, def, del, elif, else,

except, false, finally, for, from, global, if, import, in, is, lambda,

none, nonlocal, not, or, pass, raise, return, true, try, while, with,

yield

13)在乙個定義新變數中使用增值操作符(導致「nameerror: name 'foobar' is not defined」)

不要在宣告變數時使用0或者空字串作為初始值,這樣使用自增操作符的一句spam += 1等於spam = spam + 1,這意味著spam需要指定乙個有效的初始值。

該錯誤發生在如下**中:

spam = 0

spam += 42

eggs += 42

14)在定義區域性變數前在函式中使用區域性變數(此時有與區域性變數同名的全域性變數存在)(導致「unboundlocalerror: local variable 'foobar' referenced before assignment」)

在函式中使用區域性變來那個而同時又存在同名全域性變數時是很複雜的,使用規則是:如果在函式中定義了任何東西,如果它只是在函式中使用那它就是區域性的,反之就是全域性變數。

這意味著你不能在定義它之前把它當全域性變數在函式中使用。

該錯誤發生在如下**中:

somevar = 42

def myfunction():

print(somevar)

somevar = 100

myfunction()

15)嘗試使用 range()建立整數列表(導致「typeerror: 'range' object does not support item assignment」)

有時你想要得到乙個有序的整數列表,所以 range() 看上去是生成此列表的不錯方式。然而,你需要記住 range() 返回的是 「range object」,而不是實際的 list 值。

該錯誤發生在如下**中:

spam = range(10)

spam[4] = -1

也許這才是你想做:

spam = list(range(10))

spam[4] = -1

(注意:在 python 2 中 spam = range(10) 是能行的,因為在 python 2 中 range() 返回的是list值,但是在 python 3 中就會產生以上錯誤)

16)不錯在 ++ 或者 -- 自增自減操作符。(導致「syntaxerror: invalid syntax」)

如果你習慣於例如 c++ , java , php 等其他的語言,也許你會想要嘗試使用 ++ 或者 -- 自增自減乙個變數。在python中是沒有這樣的操作符的。

該錯誤發生在如下**中:

spam = 1

spam++

也許這才是你想做的:

spam = 1

spam += 1

17)忘記為方法的第乙個引數新增self引數(導致「typeerror: mymethod() takes no arguments (1 given)」)

該錯誤發生在如下**中:

class foo():

def mymethod():

print('hello!')

a = foo()

a.mymethod()

python這裡總是出現語法錯誤

還好題主自己意識到了 英語基礎的重要性 你的 是 whlie 而不是 while 呵呵,什麼錯誤,分享一下啊 python語法錯誤 python老提示語法錯誤 fun2 my fun2 小明 age 18,male home shanghai 這冒號是個什麼寫法?引數之間要使用逗號分隔而不是冒號。另...

python乙個錯誤是什麼意思?

幾個建議。1 想辦法縮短報錯的時間,如11分鐘改成10秒 3 如果2不行,逐行刪除 直到程式沒有報錯為止。執行python這個錯誤是什麼意思?你給乙個引數 help commands,然後看一下幫助。人家說你沒提供命令列引數。python 出現這個錯誤是什麼原因 蒐集了一些python最重要的內建異...

請問複製檔案出現這個錯誤是什麼意思 0x8007003B

錯誤都提示網路出現問題資料無法傳輸,導致複製中斷,這種情況經常是因為運營商那的原因,重新連線下網再試試。我的也是這個問題,win10版本1809,試了各種網上的方法,折騰了一兩天,後來在 乙太網屬性 裡勾選 internet協議版本 tcp ipv6 就好了,不一定每一臺電腦都管用,兄弟可以試一下,...