My code doesn't work in: Practice makes perfect - anti_vowel


Hey all! My code doesn’t seem to work, however it does in the shell! Here it is:)

def anti_vowel(text):
  vowels = "aeiouAEIOU"
  new_text = ''
  for i in text:
    if i not in vowels:
      new_text += i
    return new_text


Take a look at where the return is at. Remember that Python uses indentation to define the codeblocks. So what codeblock does the return belong to within your code. Is it the:

  • function
  • for loop
  • if

And when do you want to return new_text?


Hey Tonya, thanks for your reply!
Yep, my indentation is wrong!
However, nothing seems to work. My first thought after your message, that might be wrong of course, was that the return would belong to the function. However, even indenting it that way (or any other, really) it still gives an error. In the shell, with the return being alined with ‘for’, it works fine!


Hmm, let’s test that statement to see if it’s really working fine as you have it now. Put these lines of code at the bottom of the file:

print anti_vowel("Hello World!")
print anti_vowel("Does my function remove all of the vowels?")

Go ahead and run it. Does it remove all of the vowels from both of those lines? What do you think?

You’ll get back only the first character. That’s it. Why? Because the loop is only running 1 time for 1 character and that’s it.


Hi again:slight_smile:
So, I tried it again in the shell, this time putting in the code this way:

def anti_vowel(text):
 vowels = "aeiouAEIOU"
 new_text = ''
 for i in text:
   if i not in vowels:
     new_text += i
 return new_text

and it worked! But: it still doesn’t work in Codeacademy. Here is a screenshot:



Turns out, the problem was in new_text= ’ '. It worked with new_text = “” !


Aha, you had it as ` instead of a single quotation mark like '. Yes, that makes a difference. You can use both single and double quotation marks for strings.

new_text = ''  # single quotation marks
# is the same as 
new_text = "" # double quotation marks

Bravo for sticking with me in this exercise. I really wanted you to get that although it may have let you move on, you weren’t getting the right behavior out of your function with the return inside of the loop.

Good job! Keep it up!


Thanks for your guidance!


You are very welcome :smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.