Anti-Vowel


#1



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


I am getting an error message that is stating that I am returning "Hey You!" instead of "Hy Y!". My code looks like this

I expected it to remove the vowels from the new string that would be created from the input given but its not doing that so I am unsure of what to do! Please help?


Replace this line with your code.


#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

thanks!

I did this instead and it also worked!

def anti_vowel(text):
vowels = "aeiouAEIOU"
new_text = ""
for char in text:
if char not in vowels:
new_text += char
return new_text


#4

Couple of questions regarding your answer.

First, isn't str(PARAMETER) unnecessary? I just have PARAMETER in mine and it works fine. Adding str would make it work with integers and floats too i guess?

Second, since PARAMETER actually determines how many times the for loop runs, why is it okay to change PARAMETER's value within the for loop? I know the code works, just curious.


#5

Adding str() is indeed unnecessary, and the code would work fine without it. I just have it there to remind myself that I am iterating through a string and not a list, or something.

The program runs through one iteration per character in a string (that string being PARAMETER). If that character is a vowel, the program removes it from the string.

To remove a character, the program had to have checked if it was a vowel first. Meaning, it already ran an iteration. This means that a four character string with no vowels at all, would run through the same amount of iterations as a four character string with vowels.

You could even test this, by doing something like:

def FUNCTION(PARAMETER):
    for INDIVIDUAL_ITEM in str(PARAMETER):
        if INDIVDUAL_ITEM in "aeiouAEIOU":
            PARAMETER = PARAMETER.replace(INDIVIDUAL_ITEM, "")
        print "I ran an iteration."
    return PARAMETER

This will print some text to the console each time an iteration has been run, so that you can see how many iterations ran and compare that to the length of the string you gave it.


#6

Hey thanks for the responses. My next question is regarding this code in a later problem:

def purify(nums):
    for num in nums:
        if num%2 == 1:
            nums.remove(num)
        print "I think I iterated my pants"

    return nums

purify([4,5,5,4])

Output:
"I think I iterated my pants"
"I think I iterated my pants"
"I think I iterated my pants"

Also returns [4,5,4] so the second 5 is getting skipped when the list gets shortened because it takes the place of the original 5 i think.

This only iterates 3 times even though the list contains 4 items. I assume it's because one number gets removed from the list. But why does that remove an iteration when a string parameter that is shortened does not?


#7

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