全局函数未定义

Global function is not defined

我在定义我的模块时遇到了这个错误。我正在尝试通过动态编程方法编写一个用于编辑距离问题的程序。

这是我卡住的模块:

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

您的 patttern1 有三个 t。删除一个得到 pattern1.