Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Lists Disemvowel

Nikhil Alexander
Nikhil Alexander
1,444 Points

im not sure sure how to remove the letters from a string they've given.. please help me out

i understand i may not recall all of the attributes of str .... but im not able to proceed... can u correct my error??

disemvowel.py
def disemvowel(word):
    vowel.lower() = ("a", "e", "i", "o", "u")
    if vowel in word:
        return word

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 27,510 Points

There are countless ways to do this challenge, but I see where you were going with this--

You're on the right track-- we can loop through the vowels, and then exploit the replace function to replace each vowel it finds with the empty character.

Nothing that is valuable is easy-- keep plugging away at Python and your persistence will pay off.

def disemvowel(word):
    vowels = ("a", "e", "i", "o", "u")
    # step through the vowels
    for letter in vowels:
        word = word.replace(letter,'') # replace any lower-case vowel with empty char
        word = word.replace(letter.upper(),'') # replace any upper-case vowel with empty char
    return word
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,862 Points

Hi Nikhil,

You've got the right general idea but you have a few implementation errors. Let's step through your code:

     vowel.lower() = ("a", "e", "i", "o", "u")

Here you've created an iterable with the vowels in it that you can check against. And you've attempted to assign it to a variable. However, the .lower() is giving you a syntax error (you can't treat the result of a function call as a variable name). So let's just get rid of the .lower() for now (you're on the right track that we do want to make sure all the characters are lowercase, but we'll handle that elsewhere):

    vowel = ("a", "e", "i", "o", "u")

Now you have a variable against which you can test for membership. So let's move on to the conditional checking. You have

    if vowel in word:
        return word

Here you're just running your conditional once and then returning if you find the thing you're checking in word. But we know we're going to need to modify word, because the challenge says to return something without the vowels.

There's a bunch of ways we can do this, but probably the easiest is to use a for a loop to iterate through word, checking each character against your vowels list. If we are going to do it this way, then we're going to want to have a new string that we are building up with the characters we determined were not vowels. You can do this by just assigning an empty string to a variable.

Next you create the for loop. You're going to iterate through all the elements in word. On each iteration you can test if the lowercase version (where your .lower() comes in) of that element is in your vowels tuple. If it is not, then you can concatenate that element to your variable that's holding the string we're building.

Finally, once you've finished iterating through the characters in word, your variable with the temporary string will have all the characters that weren't vowels. You can then return that variable.

Hope that clears everything up.

Cheers

Alex

Nikhil Alexander
Nikhil Alexander
1,444 Points

thank you for the help.... ive understood all that you explined...it helped me write the code the code passed....thanks once again