8. anti_vowel


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/2?curriculum_id=4f89dab3d788890003000096#


My code returns "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 am not entirely sure what I am doing wrong, and though my code may not be the most elegant solution, I think that it should work.


def anti_vowel(text):
    new_text = []
    pointless_variable = 17
    for char in text:
        if(char == 'A' or char == 'E' or char == 'I' or char == 'O' or char == 'U'):
            if(char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u'):
                pointless_variable = 17
                #As the name implies, the above variable is pointless. It was solely derived in order to give my two "if" checking statements something to do, and go to the next iteration in my for loop.
        else: 
            new_text.append(char)
    return ''.join(new_text)


#2

You are overthinking this. Try following this template:

def FUNCTION(PARAMETER):
    for INDIVIDUAL_ITEM in str(PARAMETER):
        if INDIVDUAL_ITEM in "aeiouAEIOU":
            PARAMETER = PARAMETER.replace(INDIVIDUAL_ITEM, "")
    return PARAMETER

Make sure not to just follow the template and move on. I want to make sure that you understand how and why this works. If you have any questions, just ask.


#3

Isn't my code exactly that template, but elongated?


#4

Only difference is I append to a new list, and thus don't have to replace with nothing.


#6

The one other difference I see is that you convert Parameter into a string, and instead of my two if statements with lots of or operators, you use an in keyword. But besides that, I think my code is basically the same idea, with an extra list. Is there some bit of logic I'm missing?


#7

I understand why you code works, and could use it to solve the problem. However, I am also curious as to why my code doesn't work.


#8

Here:

if(char == 'A' or char == 'E' or char == 'I' or char == 'O' or char == 'U'):
    if(char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u'):
        pointless_variable = 17

The code will only run through your second if statement if the char is a capital vowel. This means that your code eliminates vowels… so long as they’re capital letters.


#9

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