未定义全局函数

2024-10-02 02:39:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

以下是我遇到的模块:

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


Tags: 模块textinpylineusersdocumentsfile

热门问题