Anti_vowel


#1



8/15 anti_vowel


I don't seem to actually be removing the vowels and I'm not sure why.

Error msg:

Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "Hey look Words!" when it should return "Hy lk Wrds!".


I expect the string text to convert to the list txt. I then try to cycle through txt and remove any vowels found. When this is finished, I'm trying to reassemble the list txt into the string text.


<img src="https://codecademy-discourse.s3.amazonaws.com/original/5X/b/d/d/0/bdd05aaa2204ca676b118cdf5bec64e669e195ea.png" width="370" height="167">

Edit:

The above image isn't showing up in my browser, so here is the code:

def anti_vowel(text):
    vowels = ['a', 'e', 'i', 'o', 'u']
    txt = list(text)
    for i in txt:
        if txt[0:] == vowels[0:4]:
            txt.remove(i)
            text = ''.join(txt)
        return text


#2

You're not comparing your txt[i] to anything so Python doesn't know which index to remove given i is your index in txt.

Check your if txt[0:] == vowels[0:4]: and make sure that you are using txt[i].

You also need to traverse your vowels list in a different way. Why not just make vowels into this:
vowels = ["aeiouAEIOU"] (consider capital cases too) and ask if text[i] in vowel?


#3

I've been tinkering with it a bit and here's what I got. I know I'm missing a piece but I can't figure out where I'm making a misstep in the logic.

def anti_vowel(text):
    vowels = ['a', 'e', 'i', 'o', 'u']
    txt = list(text.lower())
    for i in txt:
        if txt[i] == vowels[0:4]:
            txt.remove(i)
            i += 1
            text = ''.join(txt)
        return text

And here is the error code I'm getting:

Oops, try again. Does your anti_vowel function take exactly one argument (a string)? Your code threw a "list indices must be integers, not str" error.


#4

removing the [i] in txt should fix that in if txt[i] == vowels[0:4]:.


#5

I tried that and it throws a syntax error. I need some way to cycle through txt.


#6

Hmm......
You for loop should already cycle through your txt.
Oops my bad, take out the txt in txt[i] for your if statement.

Your loop specifies for every item (i) in txt, so it already cycles. That's not what you should be focusing on.
I have a feeling that something is off with your removal process. As the vowels aren't being removed.


#7

I think I had a ton of problems with the code. I took a look at the Python documentation and this is what I have now, but it's still returning the entire string, with vowels.

def anti_vowel(text):
    vowels = ['a', 'e', 'i', 'o', 'u']
    txt = list(text.lower())
    for i in txt:
        if i == vowels[0:4]:
            txt.remove(i)
            text.join(txt)
    return text

#8

Based on what you have so far, I managed to figure out a way to remove the vowels but I'm still trying to figure out how to get it to join as a string (since ''.join(txt) isn't working.).

I've never thought of this approach because of how complex it can get.


#9

I got it.

1.Make sure you vowels list contains all vowels (including capitals)

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

Don't use the .lower() as we will be returining txt instead of text (Since you aren't doing anything with text). You want to maintain the capital letters of your input.

2.Create a copy of text and make it into a list

txt = list(text)

3.When you use remove in a loop, it wont work if you have two of the same vowels. eg.
"AAk" => "Ak" instead of "k". Because remove messes up Python's internal counter. Create a copy of the list in your for loop by either doing

for i in list(txt):

or

for i in txt[:]:

As a guarantee that your .remove() works properly.
4.Remove the [0:4] after vowels in your if statement as you are comparing your list items to the whole vowels list, not the first 3 vowels.
Also, change your == to in because in checks whether your item is in the list of vowels.

if i in vowels:

5.Remove text.join(txt). Instead, use the join function with your return statement like so:

return ''.join(txt)

SIDE NOTE:
You don't necessarily need a vowels list. You can remove it and just put

if i in 'AEIOUaeiou':

I've used a different method from a topic that will be explained to you later. You can come back to it once you've learned it:

String Comprehension way (AKA most Pythonic way):

def anti_vowel(text):
    return ''.join([i for i in text if i not in 'AEIOUaeiou'])

#10

First, let me say thank you for all your help so far. You're helping me zero in on a lot of my weaknesses.

I see how this code accomplishes the task in the way I was thinking about it, but it's not taking out the final 'o' in 'Words' with the code I have based on your advice. It takes out all the other vowels and seems to logically cycle through the whole string, but still doesn't pick up that 'o':

def anti_vowel(text):
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    txt = list(text)
    for i in txt:
        if i in vowels:
            txt.remove(i)
    return ''.join(txt)

#11

Check #3 in my comment.


#12

And no problem!
I understood what you were trying to do and got curious as to how it would turn out if it worked (because as I said, I used a different method). I hope you learned a lot from this!


#13

Got it! I wasn't keeping the counter going. Fixed it by replacing "for i in txt" with "for i in txt[:]". Thanks again!

def anti_vowel(text):
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    txt = list(text)
    for i in txt[:]:
        if i in vowels:
            txt.remove(i)
    return ''.join(txt)

#14

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