av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

17個新手常見Python運(yùn)行時錯誤

當(dāng)初學(xué) Python 時,想要弄懂 Python 的錯誤信息的含義可能有點(diǎn)復(fù)雜。這里列出了常見的的一些讓你程序 crash 的運(yùn)行時錯誤。

創(chuàng)新互聯(lián)主營瑯琊網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),瑯琊h5成都小程序開發(fā)搭建,瑯琊網(wǎng)站營銷推廣歡迎瑯琊等地區(qū)企業(yè)咨詢

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

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

 
 
  1. if spam == 42 
  2.  
  3.     print('Hello!') 

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

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

 
 
  1. if spam = 42: 
  2.  
  3.     print('Hello!') 

3)錯誤的使用縮進(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ā)生在如下代碼中:

 
 
  1. print('Hello!') 
  2.  
  3.     print('Howdy!') 

或者:

 
 
  1. if spam == 42: 
  2.  
  3.     print('Hello!') 
  4.  
  5.   print('Howdy!') 

或者:

 
 
  1. if spam == 42: 
  2.  
  3. print('Hello!') 

4)在 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ā)生在如下代碼中:

 
 
  1. spam = ['cat', 'dog', 'mouse'] 
  2.  
  3. for i in range(spam): 
  4.  
  5.     print(spam[i]) 

5)嘗試修改string的值(導(dǎo)致“TypeError: ‘str’ object does not support item assignment”)

string是一種不可變的數(shù)據(jù)類型,該錯誤發(fā)生在如下代碼中:

 
 
  1. spam = 'I have a pet cat.' 
  2.  
  3. spam[13] = 'r' 
  4.  
  5. print(spam) 

而你實際想要這樣做:

 
 
  1. spam = 'I have a pet cat.' 
  2.  
  3. spam = spam[:13] + 'r' + spam[14:] 
  4.  
  5. print(spam) 

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

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

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

而你實際想要這樣做:

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

或者:

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

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

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

 
 
  1. print(Hello!') 

或者:

 
 
  1. print('Hello!) 

或者:

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

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

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

 
 
  1. foobar = 'Al' 
  2.  
  3. print('My name is ' + fooba) 

或者:

 
 
  1. spam = ruond(4.2) 

或者:

 
 
  1. spam = Round(4.2) 

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

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

 
 
  1. spam = 'THIS IS IN LOWERCASE.' 
  2.  
  3. spam = spam.lowerr() 

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

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

 
 
  1. spam = ['cat', 'dog', 'mouse'] 
  2.  
  3. print(spam[6]) 

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

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

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

12)嘗試使用Python關(guān)鍵字作為變量名(導(dǎo)致“SyntaxError:invalid syntax”)

Python關(guān)鍵不能用作變量名,該錯誤發(fā)生在如下代碼中:

 
 
  1. class = 'algebra' 

Python3的關(guān)鍵字有: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)在一個定義新變量中使用增值操作符(導(dǎo)致“NameError: name ‘foobar’ is not defined”)

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

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

 
 
  1. spam = 0 
  2.  
  3. spam += 42 
  4.  
  5. eggs += 42 

14)在定義局部變量前在函數(shù)中使用局部變量(此時有與局部變量同名的全局變量存在)(導(dǎo)致“UnboundLocalError: local variable ‘foobar’ referenced before assignment”)

在函數(shù)中使用局部變來那個而同時又存在同名全局變量時是很復(fù)雜的,使用規(guī)則是:如果在函數(shù)中定義了任何東西,如果它只是在函數(shù)中使用那它就是局部的,反之就是全局變量。

這意味著你不能在定義它之前把它當(dāng)全局變量在函數(shù)中使用。

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

 
 
  1. someVar = 42 
  2.  
  3. def myFunction(): 
  4.  
  5.     print(someVar) 
  6.  
  7.     someVar = 100 
  8.  
  9. myFunction() 

15)嘗試使用 range()創(chuàng)建整數(shù)列表(導(dǎo)致“TypeError: ‘range’ object does not support item assignment”)

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

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

 
 
  1. spam = range(10) 
  2.  
  3. spam[4] = -1 

也許這才是你想做:

 
 
  1. spam = list(range(10)) 
  2.  
  3. spam[4] = -1 

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

16)不錯在 ++ 或者 — 自增自減操作符。(導(dǎo)致“SyntaxError: invalid syntax”)

如果你習(xí)慣于例如 C++ , Java , PHP 等其他的語言,也許你會想要嘗試使用 ++ 或者 — 自增自減一個變量。在Python中是沒有這樣的操作符的。

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

 
 
  1. spam = 1 
  2.  
  3. spam++ 

也許這才是你想做的:

 
 
  1. spam = 1 
  2.  
  3. spam += 1 

17)忘記為方法的***個參數(shù)添加self參數(shù)(導(dǎo)致“TypeError: myMethod() takes no arguments (1 given)”)

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

 
 
  1. class Foo(): def myMethod(): print('Hello!') a = Foo() a.myMethod() 

分享標(biāo)題:17個新手常見Python運(yùn)行時錯誤
URL標(biāo)題:http://uogjgqi.cn/article/dhjcooe.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流