Def anti_vowel


#1



Define a function called anti_vowel that takes one string, text, as input and returns the text with all of the vowels removed.

Hi everyone, this code prints H y l k W r d s ! instead of Hy lk Wrds!
How can I remove the spaces inside the words so that my code checks with solution?

Thanks in advance!

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

anti_vowel("Hey look Words!")


#2

removing the spaces is not the only problem. Your function should return the string without vowels, even if you manage to remove the spaces, the function wouldn't return the string.

You will need a slightly different approach.


#3

Not sure I'm getting closer but I changed the code to this:

def anti_vowel(text):
    new_word = []
    for c in text:
        if c not in "aeiouAEIOU":
            new_word.append(c)
    return new_word

This time I am getting:
Oops, try again.
Your function fails on anti_vowel("Hey look Words!"). It returns
"['H', 'y', ' ', 'l', 'k', ' ', 'W', 'r', 'd', 's', '!']" when it should
return "Hy lk Wrds!".

Thank you


#4

So I got it to work using the following, however, I am having trouble seeing why the previous one does not work.

def anti_vowel(text):
    new_word = ""
    for c in text:
        if c not in "aeiouAEIOU":
            new_word += c
    return new_word

#5

You returned a list instead of a string, that's a pretty clear difference isn't it?


#6

Alright clearly need to go back a few lessons thanks @stetim94 @ionatan


#7

you could use join to get from a list to a string, a list would be a better approach since list are mutable in python, and strings are not. so when you do this:

new_word = ""
for c in text:
    if c not in "aeiouAEIOU":
        new_word += c

every time you add to string, a new string is created containing the combined string.


#8

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