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

Cant't find a way

I could't do it with remove(), so I tried with replace() but I still can't Help please Before the "or" it worked with the lowercase vowels

disemvowel.py
def disemvowel(word):
    try:
        word = word.replace("a","") or word = word.replace("A","")
        word = word.replace("e","") or word = word.replace("E","")
        word = word.replace("i","") or word = word.replace("I","")
        word = word.replace("o","") or word = word.replace("O","")
        word = word.replace("u","") or word = word.replace("U","")
    except ValueError:
        pass
    return word                                
Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

Hi Sebastian,

I've not used python in some time now, but here's how I would handle that challenge:

def disemvowel(word):
    vowels = "aeiouAEIOU";

    for char in vowels:
        word = word.replace(char, "")

    return word

There might be some funky new ways to deal with this kind of challenge in newer versions of python, but that's what I know ... I've kept your idea of string replace and just stuck it in a for loop to save typing the same thing over and over.

I hope that helps somewhat. Best of luck,

Dave

3 Answers

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Realised I posted as comment and not answer ... sigh (still getting used to the forums - soz)

Hi Sebastian,

I've not used python in some time now, but here's how I would handle that challenge:

def disemvowel(word):
    vowels = "aeiouAEIOU";

    for char in vowels:
        word = word.replace(char, "")

    return word

There might be some funky new ways to deal with this kind of challenge in newer versions of python, but that's what I know ... I've kept your idea of string replace and just stuck it in a for loop to save typing the same thing over and over.

I hope that helps somewhat. Best of luck,

Dave

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

One workaround would be to put the capital letter comparison on it's own line and skip using the or. This will pass the challenge but is very inefficient since at each replacement the entire string is copied. For small strings it's not an issue, but at longer strings performance could become an issue.

Can you think of a way to loop through each letter in the word and choose whether or not to include this letter in the results? This can be done in a singe pass through the letter and only make one copy of the word

Post back if you need more help. Good luck!!

Ben Reynolds
Ben Reynolds
35,170 Points

Hi Sebastian, I think you're best bet here is to start fresh. Although it's possible to modify your solution to produce the right string there's a ton of repetition. Whenever you find yourself writing the same code repeatedly with just a small change to each line, loops to the rescue.

Another way to remove some repetition is in the way you check for upper and lowercase versions of a character. You can just use the upper() or lower() method on the character and then use that result in your comparison.

Without giving away the code outright, I'll show you the logic I used for this challenge to hopefully help get the ball rolling:

def disemvowel(word):
    # new variable: a list of vowels, either all caps or lower, not both
    # new variable: an empty list to store the new vowel-less word
    # for each letter in word:
        # if current letter to lowercase (or upper, whichever you used above) is NOT present in the vowels list
            #add it to the new word
    # return the new word
Lindsey Cornish
Lindsey Cornish
1,043 Points

Thanks Ben, I liked the way you offered the advice without explicitly providing the code, but I found one step missing from your logic above as I completed the challenge.

Before you can return the new word you must convert your new variable back to a string before you can return the new word.

Ben Reynolds
Ben Reynolds
35,170 Points

Good point, thanks Lindsey! Yes, the new word would still be a list at the end of my loop and the characters would need to be joined before returning.

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

You raise a good point Lindsey about how advice is offered ... I always kind of debate if I should 'show a solution' or not, more often than not, I do. The problem as I see it, is that you can't please everyone. It's almost like for every person who wants to get help without seeing code, there is another who wants the code because they are just stuck and if they could only see it, they'd understand.

While I agree that it is probably better if you can figure it out yourself, I also think the nature of self-paced online courses leans more toward the concept that if people get stuck they want a solution and to move on. I mean hopefully they will think about why that solution works so they can learn from it (and possibly improve on it) but that is beyond my ability to know or do anything about.

I'm very new to the site so I don't really know how things work around here and just took my queues from other responses on the forum. Is there a preferred and/or defined format? I'm certainly open to suggestions/ideas/feedback :)

Thanking you,

Dave.

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

Oh, just had a thought on the ride to work about how I've seen this dilemma handled elsewhere.

Perhaps we could get a new entry in the Markdown Cheatsheet that allows for a solution to be added but when it's on forum they need to click a button to reveal it. That way both advice and solution can be given ... even over one or more responses ... that the person requesting can read through without fear of seeing the solution, but if they want it, it's there!

Just some feedback (or a suggestion really) for the site admin. Actually I'll take a look around and see if there is a more appropriate place to post this. Either way, thanks for getting that thought process rolling :D

Have a great day,

Dave.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Dave Harker, good point on the spoiler/ reveal markdown. We've been asking for that feature for years. Before TTH added restrictions on what was posted, we used to embed JavaScript to do the reveal. But they realized that was too dangerous to continue allowing JS in posts.