FAQ: Learn Python - Practice Makes Perfect - anti_vowel

This community-built FAQ covers the “nti_vowel” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise nti_vowel:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

8 posts were split to a new topic: Why Does This Code Fail if There Are Two or More Consecutive Vowels?

provide me solution code for anti_vowel exercise

1 Like

Hmm…

:roll_eyes:
dude
i wont bro ok.

2 Likes

Traceback (most recent call last): File “python”, line 13, in <module> File “python”, line 4, in anti_vowel IndexError: string index out of range ???

Can we see your code?

def anti_vowel(text):
for index in range(0, len(text)+1):
for t in “aeiouAEIOU”:
if text[index] == t:
#len “hey” returns 3 and since 3 is exclusive, we add 3 + 1
#we then iterate over each vowel
#remove an item if each character of the text is a vowel
rem = “”
if text[index] != t:
rem = rem + text[index]
text = rem
return text
print anti_vowel(“hey”)

The plus 1 is likely the cause of the out of range error.

def anti_vowel(text):
for index in range(0, len(text)):
for t in “aeiouAEIOU”:
if text[index] == t:
#len “hey” returns 3
#we then iterate over each vowel
#remove an item if each character of the text is a vowel
rem = “”
if text[index] != t:
rem = rem + text[index]
text = rem
else:
return text
return text

Are you now getting the results you expect without errors?

Still throws the exception I would never expect

Do you mean you are still getting the rangeError?

Can we find ways to simplify so we might narrow down the problem?

Eg.

Instead of iterating the vowel list, what if we just use the in iterator?

if text[index].lower() not in 'aeiou':
    result += text[index]

What’s more, we have no need to iterate over the indexes since we only want to poll the string characters.

for char in text:

so that the above would now read,

if char.lower() not in 'aeiou':
    result += char
1 Like
Spoilers
>>> def anti_vowel(text):
	result = ''
	for char in text:
		if char.lower() not in 'aeiou':
			result += char
	return result

>>> anti_vowel('mississippi')
'msssspp'
>>> def ante_vowel(text):
	for x in 'aeiouAEIOU':
		text = ''.join(text.split(x))
	return text

>>> ante_vowel('A quick brown fox jumps over the lazy dog.')
' qck brwn fx jmps vr th lzy dg.'
>>> 

def anti_vowel(text):
num = 0
my_arr =
my_text =""
rem = [“a”, “e”, “i”, “o”, “u”, “A”, “E”, “I”, “O”, “U”]
for char in text:
my_arr[num] = char
if char.lower() not in rem:
my_text += char
else:
my_arr.insert(num, “”)
text = “”
text += my_text
num += 1
return text
anti_vowel(“hey look words”)

1 Like

I don’t understand where the need for an array arises in this problem. It seems to add a whole lot of code that doesn’t really do anything.

>>> def anti_vowel(text):
    # num = 0
    # my_arr =
    my_text =""
    rem = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
    for char in text:
        # my_arr[num] = char
        # if char.lower() not in rem:
        if char not in rem:
            my_text += char
        # else:
        #     my_arr.insert(num, “”)
        #     text = “”
        #     text += my_text
        #     num += 1
    # return text
    return my_text

>>> anti_vowel("hey look words")
'hy lk wrds'
>>> anti_vowel('A quick brown fox jumps over the lazy dog.')
' qck brwn fx jmps vr th lzy dg.'
>>> 

We see that with all the lines removed that we do not need, the code runs as expected.

It is not necessary to use a list for the vowels. A simple string is adequate for this purpose. We will only use the .lower() method is the string is just, aeiou. If you have the capitals in the string, then drop the method.

1 Like

So I completed this exercise pretty easily and here is my code:

def anti_vowel(text):
  result = ""
  for char in text:
    if char == "a" or char == "e" or char == "i" or char == "o" or char == "u":
      result += ""
    elif char == "A" or char == "E" or char == "I" or char == "O" or char == "U":
      result += ""
    else:
      result += char
  return result

print anti_vowel('Hey! My name is Steven! I work with computers daily.')

This prints:

Hy! My nm s Stvn!  wrk wth cmptrs dly.

My question is, is there a better way to do this? I feel like this is just kinda clunky and long.

What have you learned about the in operator?

2 Likes

Even without this

then, did you have to repeat something over and over when typing that in? What? What part of it changed and what remained the same? Put the changing part in a list or other iterable, put the static part in the body of a loop. Optionally make a function out of it, testing whether x exists in y seems like something very common. After creating a function you’ve created a new more powerful verb that you can use to describe your task with. Do you need any other verbs or things to describe it better?

If I know the word filter and I know how to test for membership, then I can use that to describe antiVowel with very little code:

antiVowel = filter (`notElem` "aoeuiAOEUI")

I suppose what I’m saying is that even if you’re not aware of what exists, what would be useful? Go ahead and make it. See later if it exists, but there’s no need to wait.

1 Like

oooh I see what you mean :man_facepalming:.

1 Like