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!")

#13

I want it to print the message with vowels after it returns. Does return just stop the entire function?

vowels = "aeiouAEIOU"
extrawords = 'bcd, Hy lk Wrds!'

def anti_vowel(text):
  x = ""
  vowels = "aeiouAEIOU"
  for y in text:   #iterate y over text
    if y not in vowels:   #compare y to vowels str, if not in
  		x += y              #add y to x str
  return x
  print text
        
print anti_vowel("Hello World!  How yah doing, how yah doing, you know I get no respect.")

It seems like it does, because I was able to do this

vowels = "aeiouAEIOU"

def anti_vowel(text):
  x = ""
  vowels = "aeiouAEIOU"
  extrawords = "bcd Hey look Words!  HEY LOOK WORDS!"
  for y in text:   #iterate y over text
    if y not in vowels:   #compare y to vowels str, if not in
  		x += y              #add y to x str
  print x
  return text
        
print anti_vowel("Hello world")

#14

By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends


#15

Hi,

I read most of the posts and have found sound ways to do it there, but for the sake of learning, I’m still curious about why what I had on my first try didn’t work. I did:

def anti_vowel(text):
  lst = []
  for char in text: 
    lst.append(char)
  for item in lst:
    if item in "aeiouAEIOU":
      lst.remove(item) 
  return str("".join(lst))
  
  
  

And it returns: “Hy lk Words!” I don’t really get why it removes the vowels in the first 2 words but not in the rd? Could anyone explain that? Many thanks!


#16

the problem is actually caused by look (any word with successive vowels)

l   o   o   k
0   1   2   3

i have written the word above, with below it the indexes.

lists can’t have empty indexes, so the moment you remove the o from index 1, everything to the right of the removed item moves one slot to the left, lets visualize:

l   o   k
0   1   2

so the remove action is now done, so the loop continues to the next index, which is 2. Causing the o to be skipped (or any successive vowel for that matter)


#17

Ah that clarifies a lot thanks! Could you then enter something like a “” to put “nothing” on the removed index to continue, or would that be creating an empty index in the list and therefor this approach just doesn’t work for this problem?


#18

There are ways to solve this exercise/challenge using remove, but then you are solving problems your code created in the first place, so you should ask yourself: isn’t there a better solution?


#19

Good point. I’ll fix this in a more efficient way for now and get more into playing with indexes later. Thanks for the help!