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

please help with this one also, i have been stuck for a week.

The question wants me to remove all vowels from the word that is passed in the function [disemvowel(word)]. It also says i should check for both upper and lower cases of the vowels.

disemvowel.py
def disemvowel(word):
    if word.upper():
        word.remove("a")
        word.remove("e")
        word.remove("i")
        word.remove("o")
        word.remove("u")
    elif word.lower():
        word.remove("A")
        word.remove("E")
        word.remove("I")
        word.remove("O")
        word.remove("U")
    return word

3 Answers

Erik Linden
Erik Linden
8,913 Points

There looks like a couple of items in here to do differently. Since word is a string, I don't believe that 'remove' is something that can be called and then passed a list. It should cause an error. Also the condition is just testing the string method of upper and lower. It is frustrating sometimes when the challenges just say "Bummer". Sometimes I have to open a workspace and try editing my code snippets there, so I can see the actual error python is giving.

This solution really needs a "for" loop, and if you force each letter to lower, just for testing, then case shouldn't matter.

This might be better if you use a var to hold the return value. Then test every letter in the word against the list of vowels. Don't lower the letter except for the testing so that if the return is expected to have an upper then it still will. If the letter isn't in the vowel list, pass it to the return var.

Maybe something like this:

def disemvowel(word):
    out = ''
    vowel = ["a", "e", "i", "o", "u"]
    for l in word:
        if l.lower() in vowel:
            continue
        else:
          out+=l
    return out
Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

There are two basic issues with your solution.

  • The argument passed in is a string. A string is an immutable object. That is, it cannot be changed. The string type str does not have a remove method.

  • The method upper() returns a new copy of the string. In a true/false context of an if statement, this will always be seen as True making the elif unreachable.

Also remember that the other non-vowels in word should not change case.

A for loop works well on this challenge. Try looping over each char in the word. If the char is an upper or lower vowel, then drop it, otherwise include the char in the output. This can be by appending to a list and using join to change the list back to a string.

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

Yougang Sun
PLUS
Yougang Sun
Courses Plus Student 2,086 Points
The answer may be:
def disemvowel(word):
  vowel=['a','e','i','o','u']
  for letter in word:
    if letter.lower() in vowel:
      word.remove(letter)
  return(word)