python - Use Function to Return a List of Words Containing Required Letters -


i trying write function takes string, , checks every letter in string against every letter, in every line, in list of words. code have written is:

def uses_all(required):     fin = open('words.txt')     line in fin:         letter in line:             if letter not in required:                 pass     return line 

when try have words contain vowels returned returning last line in file.

>>> uses_all('aeiou') 'zymurgy\n' 

well, function you´ve written iterates through file without doing anything, , returns last line, behavior see kinda expected.

try this:

def uses_all(required):     ret = []     fin = open('words.txt')     line in fin:         # let´s try , find our required letters in word.         letter in required:             if letter not in line:                 break # we`re missing one! break!          else: # else block executes if no break occured             ret.append(line)      return ret 

it`s lousy implementation, should work.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -