Why does anti_vowel fail in some cases?

abusing the operator or :wink:

def anti_vowel(text):
result = “”
for i in text:
if i == ‘A’ or i == ‘a’ or i == ‘E’ or i == ‘e’ or i == ‘o’ or i == ‘O’ or i == ‘U’ or i == ‘u’:
result += " "
result += i
return result

print anti_vowel(“Hello word”)

I wrote my code this way:

def anti_vowel(text):
  for char in text:
    if char not in "aeiouAEIOU":
      print char,

anti_vowel("dog eat dog") #prints out "d g   t   d g"

The result should be acceptable, since it does remove the vowel but the site doesn’t seem to like this solution. I wonder why? Is it because of the spaces between the printed characters?

also, and you only print the anti vowel string, you need to return it

I’m not quite sure why my code doesn’t work? It seems to remove all vowels except for the one “o” in “Hey look words!” I’m not necessarily looking for a solution, I just want to know why my code doesn’t work. Thanks for any help.

def anti_vowel(text):
lst =
final = “”
for c in text:
print lst
for c in lst:
if c in “aeiouAEIOU”:
print lst
for c in lst:
final += c
print final
return final

removing from the same list as you are looping over, is a bad idea

lists can’t have empty spots/slots, so the moment you remove something from the list, everything to the right of the removed item shifts one space to the left

combined with the loop you are using (which moves to the next index), this causes letters to be skipped

vowel = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"]

def anti_vowel(text):
  for t, v in zip(text, vowel):
    if t == v:
print anti_vowel("Happy")

Hi all, thank you very much for the sharing. While I have seen how others approached the problem, it’s still not quite clear to me where I am getting wrong, the terminal delivers ‘None’. Please can somebody help? Thank you.

1 Like

None is the absence of a return value

1 Like

I think I know where I got it wrong now, zip is not appropriate here, thank you very much

1 Like

among other things, yes

1 Like

Hi, I wrote a similar code as well and got the same output. I’m not sure I quite understand your explanation. If the loops skips the second ‘o’, wouldn’t it print 'Hy lok Wrds!" instead of ‘Hy lk Words!’? Thanks!

Consider how str.remove works. It does not remove the character at the index, but the first one it encounters from left to right. The last o is the one that will remain even though it was the earlier one that was skipped over.

1 Like

Oh I understand it now, thanks a lot!

1 Like

2 posts were split to a new topic: Intermediate spoiler - anti-vowel

def anti_vowel(text):
for char in text:
if char not in “aeiouAEIOU”:
print char,

why does this not work:(

because you need to return the anti vowel string, not just print it

Is python any more efficient at running a text.replace function versus using a for loop? I was able to get my code running with the following code:

def anti_vowel(text):
newstr = text.replace(“a”,"")
newstr = newstr.replace(“e”,"")
newstr = newstr.replace(“i”,"")
newstr = newstr.replace(“o”,"")
newstr = newstr.replace(“u”,"")
newstr = newstr.replace(“A”,"")
newstr = newstr.replace(“E”,"")
newstr = newstr.replace(“I”,"")
newstr = newstr.replace(“O”,"")
newstr = newstr.replace(“U”,"")
return newstr

If we study that code we see pattern repetition in abundance. This is a good place to consider simplifying with a re-usable block.

def anti_vowel(text):
    for target in 'aeiouAEIOU':
        text = text.replace(target, '')    # re-usable block
    return text

The new pattern says everything the old repeated pattern does, but without the repetition.

Sometimes the question is more about not inducing reader fatigue than about efficiency.

My code was:

 def anti_vowel(text):
  text= " "
  for char in text:
    if char= ('a,e,i,o,u,A,E,I,O,U'):
      return text-= char

ERROR = File “python”, line 4
if char= (‘a,e,i,o,u,A,E,I,O,U’):
SyntaxError: invalid syntax

Is there a way to ‘‘remove volwes’’ differently from the code given as the answer to this??

You’ve written a sequence as a single string. The comparison will be a single character to the whole string, which will not match.

Furthermore, comparisons cannot be done with the assignment operator, but must be done with the identity relational operator., ==.

if char == ...

What you could use is a nested loop, or better still the membership operator, in.

if char in 'aeiouAEIOU':

Now the question is what is expected of the return value? A string with no vowels, is it? If that is the case there is more work to be done.

Start by declaring an empty string (be sure to use a name that is not the same as the parameter, else the input text will be lost)…

result = ""

Now iterate over the text and search for it in the vowel string.

for char in text:
    if char not in 'aeiouAEIOU':

If not in the string, then append it to the result…

result += char

At the end, return the result.

1 Like

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

def anti_vowel(text):
for i in range(len(text)-1):
for h in range(len(vowel)-1):
if text[i]==vowel[h]:

return text

print anti_vowel(“HELLO”) #returns “Hello”

Could someone tell me what i am doing wrong?Thank you ~