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 (Retired) Lists Redux Pop

This is someone else's code that I patched up so it would work, and it does but...

but I cant see where the vowels are removed. I am sleepy so I'm not thinking or seeing well but I'd love an explanation.

  • the comments are mine trying to understand why this takes out the vowels.
def devowel(word):
  new_word = ""
# iterates over the letters in word
  for i in word:
# checks for vowels in word
    if i == "a" or i == "e" or i == "o" or i == "u" or i == "y":
# if any vowels are in the word it stays the same???
      new_word = new_word
    else:
# otherwise the new word is new_word with a vowel added to it???
      new_word = new_word + i

  return new_word


print(devowel("happy"))
print(devowel("apple"))

1 Answer

andren
andren
28,526 Points
def devowel(word):
  new_word = ""
# iterates over the letters in word
  for i in word:
# checks if letter is a vowel
    if i == "a" or i == "e" or i == "o" or i == "u" or i == "y":
# if the letter is a vowel then do nothing with the letter
      new_word = new_word
    else:
# otherwise if the letter is not a vowel (this is the else statement remember) add it to new_word
      new_word = new_word + i

I updated the comments to be a bit more accurate, honestly the "new_word = new_word" seems to be there just because some code had to follow the if statement, and the author couldn't think of any other code to write that would have no effect on the flow of the program and yet make the loop continue to the next item. I don't know if this is a fault with the original author or a side effect of the fact that you adapted this code from something else, but using the continue keyword would have the same effect in this instance and be far easier to understand, as that is the actual logical thing to use in that situation.

Anyway moving on from that the rest is relatively straightforward, if the letter is a vowel the code does nothing (of any effect) and then moves on the next letter, if the letter is not a vowel then that letter is added to the new_word string.

By the end you have a string that only contains the letters that are not vowels.

continue would be a good choice there because I think it makes it more obvious that you're not doing anything there.

That being said, I would consider reversing the logic so that you can avoid having an if condition where you're not doing anything.

Something like

for letter in word:
    if letter not in ["aeiouy"]:
        new_word = new_word + letter

That's saying, if the letter is not a vowel then go ahead and add it to the end of new_word

That's a good explanation. So then this IS a rather awkward way to do this. Thanks for the input.