teoryn wrote: > I've been spending today learning python and as an exercise I've ported > a program I wrote in java that unscrambles a word. Before describing > the problem, here's the code:
> line = str.lower(line[:-1]) # convert to lowercase just in case > have to add exceptions later). The problem is when using a large > dictionary.txt file (2.9 MB is the size of the dictionary I tested) it > always gives an error, specifically: > (Note: ccehimnostyz is for zymotechnics, which is in the large > dictionary) > > > *--beginning of example--* > Enter a scrambled word : ccehimnostyz > Traceback (most recent call last): > File "unscram.py", line 62, in ? > results = dictionary[sort_string(lookup)] > KeyError: 'ccehimnostyz' > *--end of example--* If 'zymotechnics' is the last line and that line is missing a trailing newline line[:-1] mutilates 'zymotechnics' to 'zymotechnic'. In that case the dictionary would contain the key 'ccehimnotyz'. Another potential problem could be leading/trailing whitespace. Both problems can be fixed by using line.strip() instead of line[:-1] as in Robert Kern's code. Peter -- http://mail.python.org/mailman/listinfo/python-list