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 Python Collections (2016, retired 2019) Lists Disemvowel

Alexander Fullerton
Alexander Fullerton
5,225 Points

python collections - the disemvowel function can i have a hint at to why my for loop isn't doing the job?

hi there

i was wondering why my for loop is still returning vowels according to Kenneth/the computer. When I test my code in the python shell with a long word with many many vowels (both upper and lower) it removes all the vowels. Yet not enough vowels for Kenneth it seems! Why is my for loop stopping early? Would it be better to try a while loop instead? I guess I could use .index to check for vowels again and then run another loop but I'd like to have a really good for loop that did it first time - that way I'd know that I know how to write for loops!

Many Thanks!

disemvowel.py
def disemvowel(word):
    lock_on = word.lower()
    target = list(lock_on)
    for letter in target:
        if letter == 'a' or 'e' or 'i' or 'o' or 'u':
            try:
                target.remove('a')
            except ValueError:
                pass
            try:
                target.remove('e')
            except ValueError:
                pass
            try:
                target.remove('i')
            except ValueError:
                pass
            try:
                target.remove('o')
            except ValueError:
                pass
            try:
                target.remove('u')
            except ValueError:
                pass

        word = ''.join(target)

    return(word)

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

i think i see two problems that you might take a look at. one, you are modifying a list that you are looping through. this is usually a problem. it results in elements being skipped over when previous elements are removed from the list. instead, make a copy and loop through the original while modifying the copy or vice versa. second, your if statement is incorrect. if you want to check a value against several others, it would need to be if var=="a" or var=="b"...etc, not if var=="a" or "b"...like you have. better would be if var in ["a","b","c"...] using the in keyword.

Alexander Fullerton
Alexander Fullerton
5,225 Points

Thanks James! I'll have another go at it! Re your second point - naturally I was horrified to find that I had written such rubbish!

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

well i should add that the way you have your try blocks in a line like that, there is no need for a conditional at all, since whatever happens, you are removing a's, then e's etc. since your looping variable, letter, is going to be a letter in the given word, you can remove that, and cut way down on your code length with only one remove statement.