Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Show only 2-vowel words from word list; only getting the first one

def vowels(list):
    res = []
    for word in list:
        vowel_n = 0
        for x in word:
            if x in 'aeiou':
                vowel_n+=1
                
                if vowel_n== 2:
                    res.append(word)
                    return res

print(vowels(['tragedy', 'proof', 'dog', 'bug', 'blastoderm']))

Result: [‘tragedy’]

I’m expecting to show all characters that only have 2 vowels

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try reducing the indentation level of the if vowel_n== 2: block. Currently, it’s not checking the vowel counter unless the letter is a vowel. Instead of return try using break to break the inner loop

And move return to outside of the loop. You’re returning the result after the first word that matches the condition.

Check out this repl.it project: https://replit.com/@HarunYilmaz1/PrudentPalegreenScans#main.py

def vowels(list):
    res = []
    for word in list:
        vowel_n = 0
        for x in word:
            if x in 'aeiou':
                vowel_n+=1
                
            if vowel_n== 2:
                res.append(word)
                break

    return res
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading