Why does anti_vowel fail in some cases?


#1

Question

Why does anti_vowel fail in some cases?

Answer

The most common issue is returning too soon. If you’ve indented a return to be inside of a loop, that causes problems because if it’s executed, the function exits immediately and returns whatever value is there.
Be sure to return your final result outside of any loops at the end.


FAQ: Learn Python - Practice Makes Perfect - anti_vowel
#2

2 posts were split to a new topic: Anti-vowel attempting to use remove


#3

Hello I am having trouble with this assignement, my code looks like this :

def anti_vowel(text) :
  result = list(text)                # I want to work with a list
  for l in result :                  # For every item of the list
      if l in "AEIOUaeiou" :         # I check if it is a vowel
        result.remove(l)             # If it is I remove it
  return "".join(result)
  
print anti_vowel("ecureuil")         # returns "crul"  
print anti_vowel("urluberlu")        # returns "rlbrl"
print anti_vowel("Un Grand bAteAU")  # returns "n Grnd btA"
print anti_vowel("Hey lOOk words!")  # returns "Hy lOk wrds!"

Could someone tell me what I am doing wrong? Thank you in advance!

PS: Pardon my french


#4

can you see the split i made in this post/topic:

i named the new topic anti-vowel attempting to use remove, hey, what a coincidence, that is what you attempted, did you checkout this topic? That is the whole point of FAQ, to not having to repeat our selves.


#5

Hello :wink:
I think that the line {result.remove(l)} decreases the length of the list each time it gets a vowel. An alternative is to replace the line {for l in result} with {for l in text} since the length of the text does not change. :wink:


#6

Hi everyone, I looked at the answer and don’e quite get it…

def anti_vowel(text):
    t=""
    for c in text:
        for i in "ieaouIEAOU":
            if c==i:
                c=""           #How does this step removing specified strings from text? thanks
            else:
                c=c
        t=t+c
    return t

#7

Honestly, this solution is just dreadful. I know its the proposed solution, so my dreadful is not personally towards you :slight_smile:

i can highly recommend to run the code through a visualizer to see the steps:

http://www.pythontutor.com/visualize.html#mode=edit

lets try one together:

anti_vowel("me")

then we get to this line (the outer loop):

for c in text:

so for the first iteration, the c variable will have a value of "m".

then we loop through all the possible vowels, "m" won’t equal any vowel, so c variable will assign itself a value of "m" over and over again. Then after the inner loop, append to t variable (will contain result string)

then we get to second iteration of the outer loop, so c variable will get a value of "e"

the "e" will equal a vowel from the vowel string ("ieaouIEAOU"), so c variable will become an empty string. Then c variable will for the remaining iterations of the inner loop assign an empty string to itself. So that after the inner loop/nested loop, we can append (t=t+c) an empty string to t (variable containing the result)

the else clause is just completely redundant:

def anti_vowel(text):
    t=""
    for c in text:
        for i in "ieaouIEAOU":
            if c==i:
                c=""
        t=t+c
    return t
print anti_vowel("hello book")

this is already better, we could do one better:

def anti_vowel(text):
    t=""
    for c in text:
        for i in "ieaouIEAOU":
            if c==i:
                c=""
                break
        t=t+c
    return t
print anti_vowel("hello book")

no need to make more iterations once determined c variable contains a vowel

but honestly, python allows to write such elegant code:

def anti_vowel(text):
    result = ""
    vowels = "ieaouIEAOU"
    for char in text:
          if char not in vowels:
            result += char
    return result
print anti_vowel("hello book")

its almost like reading english, if char not in vowels, you read that, you instantly understand what the line does. By using python simple not in operations + logic variable names, it makes such a difference

i would prefer to use lists for result, but you get the idea that this problem has a much more elegant way to be solved


#8

Got it, thanks stetim94


#9

The solution has actually been changed inside the lesson, it should now give:

def anti_vowel(text):
    result = ""
    vowels = "ieaouIEAOU"
    for char in text:
          if char not in vowels:
            result += char
    return result

#10

Thank you! I made the same mistake so your comment was very helpful :slight_smile:


#11

i copied your code to my editor then run
and it works successfully .


#12

We can find the index of deleted vowel and replace it for “”:

def anti_vowel(text):
  vowel = list("ieaouIEAOU")
  result = list(text)
  #text = "Hey look Words!"
  for lit in result:
    if lit in vowel:      
      i = result.index(lit)
      result.remove(lit)
      result.insert(i, "")
  return "".join(result)  
print anti_vowel("Hey look Words!")