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 (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,558 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.