[Tutor] Python 2.7.1 interpreter complains about NameError: global name 'levenshtein_automata' is not defined

Alan Gauld alan.gauld at btinternet.com
Tue Dec 28 08:42:42 CET 2010


"Frank Chang" <frankchang91 at gmail.com> wrote

>  Good morning, I am using Python 2.7.1 on Windows XP Service Pack 3. 
> Here
> is the program where the Python interpreter complains about 
> NameError:
> global name 'levenshtein_automata' is not defined.

The others have answered the specific question, however I think you
have another problem lurking:

> def find_all_matches(self, word, k, lookup_func):
>    lev = levenshtein_automata(word, k).to_dfa()
>    match = lev.next_valid_string('\0')
>    while match:
>        next = lookup_func(match)
>        if not next:
>           return

Here you return a None value

>        if match == next:
>          yield match
>          next1 = next1 + '\0'
>       match = lev.next_valid_string(next1)
>
> length = len(list(testdfa.find_all_matches('food', 1, m))) ######## 
> line 174

And here you try to convert the return value to a list.
But list(None) will fail with a TypeError.
You should probably returm an empty string or raise an exception
and catch it in your main block.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list