Anti_Vowel (Help please)


#1


When run the error says only H is returned from "Hey Look Words!"

I don't understand why it only spits out the first letter.

def anti_vowel(text):
    for i in range(0, len(text) - 1): 
        n = ""
        vowels = ["aeiouAEIOU"]
        
        for j in range(len(vowels) - 1):  
            if text[i] != vowels[j]:
                j += 1
                del text[i]
                
        else:
            n = n + text[i]
        return n


#2

When it comes to programming and well in fact most things. Simplicity is the best option.

There is no need for two for loops, a del statement, and two variables, an if != statement and an integer in the appending and for loops.

def anti_vowel(text):
    n = " "
    for i in (text): 
        if i not in "aeiouAEIOU":
            n += i
    return n
print(anti_vowel("Aeiou aeiou hello"))

Simply put.

If you need any further explanation about the code above, feel free to ask. :slight_smile:


#3

Thanks a lot for your help. I know simplicity is better I just haven't seen the "not in " part ever used so I kinda had to a take more complex route. So "not in" just takes the place of my second for loop basically?


#4

Well no not exactly the two for loops are still independent but when writing bigger functions or programs minimalism is key.


#5

I think this one is the fastest solution - guys on old forums propose much more complex code when in fact it's only 5 lines

def anti_vowel(text):
    for c in text:
        if c in "aeiuoAEIUO":
            text = text.replace(c, "")
    return text

#6

I believe your program is only returning the first letter because your return n line is inside of your for loop. I did this myself when I was just going through the problem.

It seems others have focused on alternate solutions, but I figured I would point out the reason why your code was returning only that single character.


#7

I am getting following error though output does not have vowels and it prints Hy lk Wrds! . i tried it on local python and on codeacademy. i am frustrated as same thing happened in case of REVERSE order. it did print !nohtyP correctly


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



#8

What version of python are you using?


#9

SOLVED! i was printing in function instead of returning value. thank for suggestion.


#10

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