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 trialAdam Tyler
14,865 PointsDisemvowel Challenge
Am I using the for loop incorrectly? I used this script in workspaces and ran it with : "disemvowel('cheese PLEASE')" This returned: "chse PLAS"
Not sure why it works on some of the vowels but not all, Thank you in advanced.
def disemvowel(word):
letters = list(word)
vowels = ['a','e','o','u', 'A', 'E', 'I', 'O', 'U']
for letter in letters:
if letter in vowels:
letters.remove(letter)
else:
pass
word = ''.join(letters)
return(word)
1 Answer
Steve Hunter
57,712 PointsHi Adam,
This is because you're deleting items from the array but the loop will carry on to the 'next' element. (And there's no lower case i in your list!)
So, you remove element 4, the loop moves on to element 5 but this is now what used to be element 6 as you deleted the 4th.
Make sense?
You're better approaching this by building a list of non-vowels, rather than deleting.
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsTake the word
TEAL
. The elements are:After the second pass, you delete [1] and the loop continues onto [2] but the remaining string is now:
So you skip testing the A because you moved over it by deleting the element.
Best leave everything in place and build a new string of non-vowels.
Adam Tyler
14,865 PointsAdam Tyler
14,865 PointsThank you, I have completed the challenge now!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsGood work!