① 本人python新手,現在在看python基礎教程,這幾天敲了一下後面的項目5,出現了很奇怪的錯誤,大神幫看看
Room.add(self)這一句改成
Room().add()
當然,只是一個比喻,具體怎麼樣改,還要看你的程序。
Room是一個類,除非它有特殊的方法。否則不能使用Room.add這樣的方法,在python不讓。
你可以先實例化Room,比如Room(),然後在這個實例上就可以使用權Room().add()
此外你Room.add(self)里的self顯然不是Room實例,否則也許能成功。
② python 這個報錯怎麼解決
展開全部
python新手常見的報錯提示
在運行或編寫一個程序時常會遇到錯誤異常,這時Python會給你一個錯誤提示類名,告訴出現了什麼樣的問題(Python是面向對象語言,所以程序拋出的異常也是類)。能很好的理解這些錯誤提示類名所代表的意思,可以幫助你在最快的時間內找到問題所在,從而解決程序上的問題是非常有幫助的。
搜集了一些python最重要的內建異常類名,並做了簡單的介紹:
AttributeError:屬性錯誤,特性引用和賦值失敗時會引發屬性錯誤
NameError:試圖訪問的變數名不存在
SyntaxError:語法錯誤,代碼形式錯誤
Exception:所有異常的基類,因為所有python異常類都是基類Exception的其中一員,異常都是從基類Exception繼承的,並且都在exceptions模塊中定義。
IOError:一般常見於打開不存在文件時會引發IOError錯誤,也可以解理為輸出輸入錯誤
KeyError:使用了映射中不存在的關鍵字(鍵)時引發的關鍵字錯誤
IndexError:索引錯誤,使用的索引不存在,常索引超出序列范圍,什麼是索引
TypeError:類型錯誤,內建操作或是函數應於在了錯誤類型的對象時會引發類型錯誤
ZeroDivisonError:除數為0,在用除法操作時,第二個參數為0時引發了該錯誤
ValueError:值錯誤,傳給對象的參數類型不正確,像是給int()函數傳入了字元串數據類型的參數。
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 aninteger」)
通常你想要通過索引來迭代一個list或者string的元素,這需要調用 range() 函數。要記得返回len 值而不是返回這個列表。
該錯誤發生在如下代碼中:
spam = ['cat','dog', 'mouse']
for i inrange(spam):
print(spam[i])
5)嘗試修改string的值(導致「TypeError: 'str' object does not support itemassignment」)
string是一種不可變的數據類型,該錯誤發生在如下代碼中:
spam = 'I have apet cat.'
spam[13] = 'r'
print(spam)
而你實際想要這樣做:
spam = 'I have apet cat.'
spam = spam[:13] +'r' + spam[14:]
print(spam)
6)嘗試連接非字元串值與字元串(導致 「TypeError: Can't convert 'int' object to strimplicitly」)
該錯誤發生在如下代碼中:
numEggs = 12
print('I have ' +numEggs + ' eggs.')
而你實際想要這樣做:
numEggs = 12
print('I have ' +str(numEggs) + ' eggs.')
或者:
numEggs = 12
print('I have %seggs.' % (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 INLOWERCASE.'
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 ofmy 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' referencedbefore assignment」)
在函數中使用局部變來那個而同時又存在同名全局變數時是很復雜的,使用規則是:如果在函數中定義了任何東西,如果它只是在函數中使用那它就是局部的,反之就是全局變數。
這意味著你不能在定義它之前把它當全局變數在函數中使用。
該錯誤發生在如下代碼中:
someVar = 42
def myFunction():
print(someVar)
someVar = 100
myFunction()
15)嘗試使用 range()創建整數列表(導致「TypeError: 'range' object does not support itemassignment」)
有時你想要得到一個有序的整數列表,所以 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錯誤:unexpected unident
這種情況,是縮進有錯誤的,找找哪裡沒對齊。
python依靠indent來縮進。要麼全部用空格縮進,要麼全部tab鍵。用有些編輯器可以顯示tab和空格的。另外,最好不要放到帶中文的目錄下,防止出問題。
(3)unnealpython擴展閱讀:
Python(計算機程序設計語言)
Python 是一門有條理的和強大的面向對象的程序設計語言,類似於Perl, Ruby, Scheme, Java.
發展歷程
自從20世紀90年代初Python語言誕生至今,它已被逐漸廣泛應用於系統管理任務的處理和Web編程。
Python的創始人為Guido van Rossum。1989年聖誕節期間,在阿姆斯特丹,Guido為了打發聖誕節的無趣,決心開發一個新的腳本解釋程序,作為ABC 語言的一種繼承。之所以選中Python(大蟒蛇的意思)作為該編程語言的名字,是因為他是一個叫Monty Python的喜劇團體的愛好者。
ABC是由Guido參加設計的一種教學語言。就Guido本人看來,ABC 這種語言非常優美和強大,是專門為非專業程序員設計的。但是ABC語言並沒有成功,究其原因,Guido 認為是其非開放造成的。Guido 決心在Python 中避免這一錯誤。同時,他還想實現在ABC 中閃現過但未曾實現的東西。
就這樣,Python在Guido手中誕生了。可以說,Python是從ABC發展起來,主要受到了Mola-3(另一種相當優美且強大的語言,為小型團體所設計的)的影響。並且結合了Unix shell和C的習慣。
Python已經成為最受歡迎的程序設計語言之一。自從2004年以後,python的使用率呈線性增長。2011年1月,它被TIOBE編程語言排行榜評為2010年度語言。
Python_網路
④ python 問題
os、shutil這兩個庫比較特殊,一般都是直接import,較少使用from
importos
importshutil
fn='xxx.pdf'
basedir='G:\tqcs'
forfiinos.listdir(os.path.join(basedir,'sr')):
iffn==fi:
sr=os.path.join(basedir,'sr',fi)
sc=os.path.join(basedir,'sc',fi)
shutil.(sr,sc)
else:
q=os.path.splitext(fn)[0]
withopen(os.path.join(basedir,'msmj.txt'),'w')asf:
f.write(q)
優化後:
importos
importshutil
fn='xxx.pdf'
basedir='G:\tqcs'
shutil.(os.path.join(basedir,'sr',fn),
os.path.join(basedir,'sc',fn))
unfiles=[os.path.splitext(fi)[0]forfiin
os.listdir(os.path.join(basedir,'sr'))iffi!=fn]
withopen(os.path.join(basedir,'msmj.txt'),'w')asf:
f.write(' '.join(unfiles))
⑤ Python中的undraw怎麼說
你說的是python graphics庫中的函數吧。
官方文檔:
undraw() Undraws the object from a graphics window, This proces an error if the object is not currently drawn.
就是說,從圖形窗口移除一個對象,如果當前圖形窗口中沒有這個對象會報錯。
如果解決了您的問題請採納!
如果未解決請繼續追問
⑥ 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 = {'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 = 0
spam += 42
eggs += 4214)在定義局部變數前在函數中使用局部變數(此時有與局部變數同名的全局變數存在)(導致「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 += 117)忘記為方法的第一個參數添加self參數(導致「TypeError: myMethod() takes no arguments (1 given)」)
該錯誤發生在如下代碼中:
class Foo():
def myMethod():
print('Hello!')
a = Foo()
a.myMethod()
⑦ 求python入門教程python基礎教程
我這里有Python編程,開發,進階,自動化,實戰等系列視頻教程,還有安裝包,素材,代碼等等內容,這里就不一一說了,全套內容一共300G左右,非常的詳細,需要的請到網盤下載查看整體內容,提取碼:nuq6
⑧ 什麼是python操作系統
Python屬於腳本語言,可以用於人工智慧、游戲開發、自動化運維、自動化測試、後端開發等領域,所謂的Python操作系統就是用Python來寫的,Python是底層
⑨ python中出現IndentationError:unindent does not match any outer indentation level是什麼問題
Python語法要求在一份運行代碼中的所有for,if/else的語句':'的下一行要統一縮進量(有』一個製表符(TAB鍵)『,』兩個空格『,和』三個空格『可以選擇)
如果縮進量不統一(比如TAB鍵和四個空格混用),則程序便會報錯IndentationError: unindent does not match any outer indentation level(縮進不匹配任何外在的縮進級別)
需要注意的是,即使Tab鍵的長度和四個空格一樣長,兩者一起用依然還是會報錯。。。
這種情況下很難查出錯,就需要使用文本編輯器裡面的顯示空格與製表符的功能來查看了。
如圖,點擊notepad++環境下的視圖->顯示符號->顯示空格與製表符路徑,便可以查看了
Tab鍵在這里顯示為一個箭頭,而空格顯示為一個點。這樣便知道哪裡用了Tab鍵,哪裡用了四個空格了,之後統一一下就不會報錯了。
(9)unnealpython擴展閱讀
可選的 n 參數是在顯示或列印列表中的下一個表達式之前移動的列數。若省略此參數,則 Tab 將插入點移動到下一個列印區的起點。這就使 Tab 可用來替換國別中的逗號,此處,逗號是作為十進制分隔符使用的。
import sysimport readlineimport rlcompleterimport atexitimport os#tab
completionreadline.parse_and_bind('tab:complete')#history filehistfile =
os.path.join(os.environ['HOME'],'.pythonhistory')try:
readline.read_history_file(histfile)except IOError:
passatexit.register(readline.write_history_file,histfile)del os,histfile,readline,rlcompleter
⑩ Python中此類錯誤如何修改
請注意代碼的縮進
unprinted=['iphone','huawei','xiaomi']
completed=[]
defprinted(unprinted,completed):
whileunprinted:
current=unprinted.pop()
print('printingmodel'+current)
completed.append(current)
defshow(completed):
print(' thefollowingmodels:')
forcompletedsincompleted:
print(completeds)
printed(unprinted,completed)