So for some background: I’ve been going through learning python the hard way and have taken a little break to try doing a few fun things, I came across on a suggestion on daniweb, to try create a program in which you enter in a list of characters and it will then print out any word that contain all those characters.
I’ve figured out how to do it manually, here’s the below code:
string = raw_input("Please enter the scrable letters you have: ")
for line in open('/usr/share/dict/words', 'r').readlines():
if string[0] in line and string[1] in line and string[2] in line:
print line,
But I somehow cannot figure out how to get it to work by using loops (that way the user can enter in a list of characters of any length. I figured something like the below would work, but it doesn’t appear to do so:
while i < len(string)-1:
if string[i] in line: tally = tally + 1
i = i + 1
if tally == len(string)-1: print line
else: i = 0
Any help in the right direction would be much appreciated, thanks.
Set operations can come in handy here: