python 學習筆記01

python學習過程遇到一些問題記錄:html


一、 IndentationError:expected an indented block錯誤的解決辦法python

一句話 有冒號的下一行每每要縮進,該縮進就縮進app

參考資料:http://blog.csdn.net/hongkangwl/article/details/16344749
curl


二、17個新手常見Python運行時錯誤ide

初學 Python 時,想要弄懂 Python 的錯誤信息的含義可能有點複雜。這裏列出了常見的的一些讓你程序 crash 的運行時錯誤。函數

1)忘記在 if , elif , else , for , while , class ,def 聲明末尾添加 :(致使 「SyntaxError :invalid syntax」)工具

該錯誤將發生在相似以下代碼中:post

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

2)使用 = 而不是 ==(致使「SyntaxError: invalid syntax」)學習

 = 是賦值操做符而 == 是等於比較操做。該錯誤發生在以下代碼中:ui

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 = 12print('I have ' + numEggs + ' eggs.')

而你實際想要這樣作:

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

或者:

numEggs = 12print('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 = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'}
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 = 0spam += 42eggs += 42

14)在定義局部變量前在函數中使用局部變量(此時有與局部變量同名的全局變量存在)(致使「UnboundLocalError: local variable 'foobar' referenced before assignment」)

在函數中使用局部變來那個而同時又存在同名全局變量時是很複雜的,使用規則是:若是在函數中定義了任何東西,若是它只是在函數中使用那它就是局部的,反之就是全局變量。

這意味着你不能在定義它以前把它當全局變量在函數中使用。

該錯誤發生在以下代碼中:

someVar = 42def myFunction():
    print(someVar)
    someVar = 100myFunction()

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 = 1spam++

也許這纔是你想作的:

spam = 1spam += 1

17)忘記爲方法的第一個參數添加self參數(致使「TypeError: myMethod() takes no arguments (1 given)」)

該錯誤發生在以下代碼中:

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

三、python 導入模塊出錯 ImportError: No module named 'request'

import request

解決方法:pip install requests

四、Python3中,已經安裝了bs4(Beautifulsoup 4)了,可是卻仍是出錯:ImportError: No module named BeautifulSoup

BeautifulSoup 的安裝:
安裝方法:
1 : apt-get install python-bs4
2 : easy_install beautifulsoup4
3 : pip install beautifulsoup4
4 :源碼安裝: python setup.py install
 根據不一樣的操做系統,選用不一樣的安裝方法,這些方法都能安裝成功,不一樣點在於安裝的工具不一樣。
 採用的是第四種安裝方法,下面我來簡要介紹下第四種安裝方法:
 curl http://www.crummy.com/software/BeautifulSoup/bs4/download/4.1/beautifulsoup4-4.1.2.tar.gz >> beautifulsoup4-4.1.2.tar.gz  
tar zxvf beautifulsoup4-4.1.2.tar.gz  
cd beautifulsoup4-4.1.2  
python setup.py install  
Ok ,你就能看到安裝信息,提示安裝成功。
安裝成功,確定想火燒眉毛的使用,你打開 python command 窗口,你很 happy 的輸入:

參考資料:

http://isilic.iteye.com/blog/1741918

http://www.crifan.com/python3_after_install_bs4_still_error_importerror_no_module_named_beautifulsoup/

Beautiful×××地址:

https://www.crummy.com/software/BeautifulSoup/bs4/download/

python多版本共存問題

http://www.cnblogs.com/Yiutto/p/5962906.html

相關文章
相關標籤/搜索