[Tutor] help with loop that is to be fed out of a word list

David ldl08 at gmx.net
Tue Feb 10 05:37:55 CET 2009


Dear list,

out of "Thinking in Python" I take the following code, which
"takes a word and a string of required letters, and that returns True if
the word uses all the required letters at least once".


def uses_all(word, required):
    for letter in required:
        if letter not in word:
            return False
    return True

Now, I want to feed this code a list of words. This is what I have so far:


def uses_all(required):
    fin = open('words.txt')
    for line in fin:
        word = line.strip()
        for letter in required:
            if letter not in word:
                # print "False!"
                return False
        print word

required = raw_input("what letters have to be used? ")
print required

uses_all(required)


The code runs, but does not print the words. All I get it the output of
the 'print required' command:

david at ubuntu:~/Documents/Tools/Python/Code$ python downey_9.5.py
what letters have to be used? zhg
zhg
aa
z
david at ubuntu:~/Documents/Tools/Python/Code$

I realise that my loop fails to execute beyond the first word in the
list ("aa"), but why?	

Thanks in advance for your insights!

David



More information about the Tutor mailing list