Search code examples
pythonfunctionfor-loopreturn

Need to write a function that returns all of the vowels in a word


I am new to learning python and am struggling to create a Function to return a string only of vowels from word no matter if its a blank string, string with no vowels, or string with all vowels.

This is what I wrote:

def vowels_only(word):
    word = "banana"
    vowels = 'aeiouy'
    for letters in word:
        if letters == vowels:
            return letters

I expected to get: "aaa"

actual: None

What did I do wrong?


Solution

  • You'll have to check each letter against the values in vowels.

    def vowels_only(word):
        vowels = list('aeiouy')
        letters = []
        for letter in word:
            if letter in vowels and letter not in letters:
                letters.append(letter)
        return letters