Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How to get vowels from a set of words in a text file

Im trying to get 10 vowels in a word from a text file. I realize the issue here is within my get_vowels function, but im not seeing how exactly to fix it. Any help would be appreciated

fin = open('\scrabble\words.txt')

def get_a_word(): line = fin.readline() word = line.strip() return word

def get_vowels(word): num_of_v = 0 for i in range(len(word)): if word[i] == 'a' or 'e' or 'i' or 'o' or 'u' or 'y' and not 'w': num_of_v = num_of_v + 1 if num_of_v == 10: return True else: return False

def print_two_words(): for i in range(113810): word = get_a_word() if get_vowels(word): print(word)

print_two_words()

Steven Parker
Steven Parker
231,269 Points

To preserve your indentation, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

1 Answer

Steven Parker
Steven Parker
231,269 Points

Even with the code unformatted, this line stands out:

 if word[i] == 'a' or 'e' or 'i' or 'o' or 'u' or 'y' and not 'w':

You can use logic ("and", "or") to combine complete expressions, but not to make a single comparison work on multiple terms. So to make this work, you'd need to finish the expressions:

 if word[i] == 'a' or word[i] == 'e' or word[i] == 'i' or word[i] == 'o' or #...etc

But a more compact way to do this kind of test is with the membership operator:

 if word[i] in 'aeiou':  # 'y' and not 'w' ??

I didn't understand what the last part of the test was intended to do so I left it off this example.