日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
初學(xué)Python常見異常錯(cuò)誤,總有一處你會(huì)遇到!

初學(xué)Python常見錯(cuò)誤

  1. 忘記寫冒號(hào)
  2. 誤用=
  3. 錯(cuò)誤 縮緊
  4. 變量沒有定義
  5. 中英文輸入法導(dǎo)致的錯(cuò)誤
  6. 不同數(shù)據(jù)類型的拼接
  7. 索引位置問題
  8. 使用字典中不存在的鍵
  9. 忘了括號(hào)
  10. 漏傳參數(shù)
  11. 缺失依賴庫
  12. 使用了python中對(duì)關(guān)鍵詞
  13. 編碼問題

1. 忘記寫冒號(hào)

在 if、elif、else、for、while、def語句后面忘記添加 :age = 42if age == 42 print('Hello!')

創(chuàng)新互聯(lián)公司是一家專業(yè)從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)的品牌網(wǎng)絡(luò)公司。如今是成都地區(qū)具影響力的網(wǎng)站設(shè)計(jì)公司,作為專業(yè)的成都網(wǎng)站建設(shè)公司,創(chuàng)新互聯(lián)公司依托強(qiáng)大的技術(shù)實(shí)力、以及多年的網(wǎng)站運(yùn)營經(jīng)驗(yàn),為您提供專業(yè)的成都網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)及網(wǎng)站設(shè)計(jì)開發(fā)服務(wù)!

 
 
 
 
  1. age =  42 
  2. if age ==  42     
  3. print ( 'Hello!' )    
  4. File "" , line  2     
  5. if age == 42
  6.               ^ 
  7. SyntaxError : invalid syntax

2. 誤用 =

= 是賦值操作,而判斷兩個(gè)值是否相等是 ==

 
 
 
 
  1. gender = '男' 
  2. if gender = '男' :    
  3. print ( 'Man' )  
  4. File "" , line  2     
  5. if  gender =  '男' : 
  6.               ^ 
  7. SyntaxError : invalid syntax

3. 錯(cuò)誤的縮進(jìn)

Python用縮進(jìn)區(qū)分代碼塊,常見的錯(cuò)誤用法:

 
 
 
 
  1. print('Hello!') 
  2. print('Howdy!') 
  3.     File "", line 2 
  4.     print('Howdy!') 
  5.       ^ 
  6. IndentationError: unexpected indent 
  7. num = 25 
  8. if num == 25: 
  9.       print('Hello!') 
  10.     File "", line 3 
  11.     print('Hello!') 
  12.        ^ 
  13. IndentationError: expected an indented block

4. 變量沒有定義

 
 
 
 
  1. if city in ['New York', 'Bei Jing', 'Tokyo']: print('This is a mega city') 
  2. --------------------------------------------------------------------------- 
  3. NameError Traceback (most recent call last) in 
  4. ----> 1 if city in ['New York', 'Bei Jing', 'Tokyo']: 
  5.         2 print('This is a mega city') 
  6. NameError: name 'city' is not defined

5. 中英文輸入法導(dǎo)致的錯(cuò)誤

  • 英文冒號(hào)
  • 英文括號(hào)
  • 英文逗號(hào)
  • 英文單雙引號(hào)
 
 
 
 
  1. if 5>3: 
  2.     print('5比3大') 
  3.    File "", line 1 
  4.    if 5>3: 
  5.           ^ 
  6. SyntaxError: invalid character in identifier 
  7. if 5>3: 
  8.      print('5比3大') 
  9.    File "", line 2 
  10.     print('5比3大') 
  11.                  ^ 
  12. SyntaxError: invalid character in identifier 
  13. spam = [1, 2,3] 
  14.     File "", line 1 
  15.     spam = [1, 2,3] 
  16.                  ^ 
  17. SyntaxError: invalid character in identifier 
  18. if 5>3: 
  19.      print('5比3大‘) 
  20.    File "", line 2 
  21.     print('5比3大‘) 
  22.                ^ 
  23. SyntaxError: EOL while scanning string literal

6. 不同數(shù)據(jù)類型的拼接

字符串/列表/元組 支持拼接

字典/集合不支持拼接

 
 
 
 
  1. 'I have ' + 12 + ' eggs. 
  2. '#'I have {} eggs.'.format(12) 
  3. --------------------------------------------------------------------------- 
  4. TypeError                  Traceback (most recent call last) in 
  5. ----> 1 'I have ' + 12 + ' eggs.' 
  6. TypeError: can only concatenate str (not "int") to str 
  7. ['a', 'b', 'c']+'def' 
  8. --------------------------------------------------------------------------- 
  9. TypeError                     Traceback (most recent call last) in 
  10. ----> 1 ['a', 'b', 'c']+'def' 
  11. TypeError: can only concatenate list (not "str") to list 
  12. ('a', 'b', 'c')+['a', 'b', 'c'] 
  13. --------------------------------------------------------------------------- 
  14. TypeError                     Traceback (most recent call last) in 
  15. ----> 1 ('a', 'b', 'c')+['a', 'b', 'c'] 
  16. TypeError: can only concatenate tuple (not "list") to tuple 
  17. set(['a', 'b', 'c'])+set(['d', 'e']) 
  18. --------------------------------------------------------------------------- 
  19. TypeError                   Traceback (most recent call last) in 
  20. ----> 1 set(['a', 'b', 'c'])+set(['d', 'e']) 
  21. TypeError: unsupported operand type(s) for +: 'set' and 'set' 
  22. grades1 = {'Mary':99, 'Henry':77} 
  23. grades2 = {'David':88, 'Unique':89} 
  24. grades1+grades2 
  25. --------------------------------------------------------------------------- 
  26. TypeError             Traceback (most recent call last) in  
  27.         2 grades2 = {'David':88, 'Unique':89} 
  28.         3 
  29. ----> 4 grades1+grades2 
  30. TypeError: unsupported operand type(s) for +: 'dict' and 'dict'

7. 索引位置問題

 
 
 
 
  1. spam = ['cat', 'dog', 'mouse']
  2. print(spam[5]) 
  3. --------------------------------------------------------------------------- 
  4. IndexError                    Traceback (most recent call last) in 
  5. 1 spam = ['cat', 'dog', 'mouse']
  6. ----> 2 print(spam[5]) 
  7. IndexError: list index out of range

8. 使用字典中不存在的鍵

在字典對(duì)象中訪問 key 可以使用 [],

但是如果該 key 不存在,就會(huì)導(dǎo)致:KeyError: 'zebra'

 
 
 
 
  1. spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'} 
  2. print(spam['zebra']) 
  3. --------------------------------------------------------------------------- 
  4. KeyError                 Traceback (most recent call last) in 
  5.         3 'mouse': 'Whiskers'} 
  6.         4 
  7. ----> 5 print(spam['zebra']) 
  8. KeyError: 'zebra'

為了避免這種情況,可以使用 get 方法

 
 
 
 
  1. spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'} 
  2. print(spam.get('zebra')) 
  3. None

key 不存在時(shí),get 默認(rèn)返回 None

9. 忘了括號(hào)

當(dāng)函數(shù)中傳入的是函數(shù)或者方法時(shí),容易漏寫括號(hào)

 
 
 
 
  1. spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'} 
  2. print(spam.get('zebra') 
  3. File "", line 5 
  4. print(spam.get('zebra') 
  5.                     ^ 
  6. SyntaxError: unexpected EOF while parsing

10. 漏傳參數(shù)

 
 
 
 
  1. def diyadd(x, y, z): return x+y+zdiyadd(1, 2) 
  2. --------------------------------------------------------------------------- 
  3. TypeError                Traceback (most recent call last) in 
  4.                  2 return x+y+z 
  5.                  3 
  6.           ----> 4 diyadd(1, 2) 
  7. TypeError: diyadd() missing 1 required positional argument: 'z'

11. 缺失依賴庫

電腦中沒有相關(guān)的庫

12. 使用了python中的關(guān)鍵詞

如try、except、def、class、object、None、True、False等

 
 
 
 
  1. try = 5print(try) 
  2. File " ", line 1 
  3. try = 5
  4. SyntaxError: invalid syntax 
  5. def = 6 
  6. print(6) 
  7. File "", line 1 
  8. def = 6 
  9. SyntaxError: invalid syntax

13. 文件編碼問題

 
 
 
 
  1. import pandas as pd 
  2. df = pd.read_csv('data/twitter情感分析數(shù)據(jù)集.csv') 
  3. df.head()

嘗試encoding編碼參數(shù)傳入utf-8、gbk

 
 
 
 
  1. df = pd.read_csv('data/twitter情感分析數(shù)據(jù)集.csv', encoding='utf-8') 
  2. df.head()

都報(bào)錯(cuò)說明編碼不是utf-8和gbk,而是不常見都編碼,這里我們需要傳入正確都encoding,才能讓程序運(yùn)行。

python有個(gè)chardet庫,專門用來偵測編碼。

 
 
 
 
  1. import chardet 
  2. binary_data = open('data/twitter情感分析數(shù)據(jù)集.csv', 'rb').read() 
  3. chardet.detect(binary_data) 
  4. {'encoding': 'Windows-1252', 'confidence': 0.7291192008535122, 'language': ''} 

網(wǎng)站名稱:初學(xué)Python常見異常錯(cuò)誤,總有一處你會(huì)遇到!
網(wǎng)頁URL:http://www.dlmjj.cn/article/coohiog.html