我在定義我的模塊時遇到了此錯誤。我正嘗試通過動態編程方法編寫一個編輯距離問題的程序。全局函數未定義
這裏就是我堅持的模塊:
def cost(i,j,M,w,text,pattern,compare): #Defining the cost functions or can say recurrence formula
M[0,j]=0
text1=list(text)
pattern1=list(pattern)
for i in range(1,m+1):
for j in range(1,n+1):
insertions = M[i-1,j]+1
deletions = M[i,j-1]+1
matches=M[i-1,j-1]
if text1[i]==patttern1[j]:
matches = matches+1
return matches
else :
return matches
和錯誤是:
Traceback (most recent call last): File "/Users/sayaneshome/Documents/plschk.py", line 202, in fill(M, w, text, max) #Filling matrix M with scores File "/Users/sayaneshome/Documents/plschk.py", line 117, in fill c = cost(i,j,M,w,text,pattern,compare) File "/Users/sayaneshome/Documents/plschk.py", line 95, in cost if text1[i]==patttern1[j]: NameError: global name 'patttern1' is not defined
下次您是否可以通過[PEP8 online](http://pep8online.com/)運行您的代碼並在發佈之前修復所有錯誤?分析函數後[代碼結果](http://pep8online.com/s/yq7rGhb5)顯示22個錯誤。 –
Okk ..我會這樣做的。 –
嗨,這是代碼的一部分...因爲我無法粘貼整個程序在這裏,所以也許這個錯誤的原因.. –