Anti-vowel: Can you help and explain why it’s error?


#1

Hi, Can you help and explain why it’s error? the output is always starting with "bcd"
and it displays as wrong.

def anti_vowel(text):
new_word=""
for i in text:
if i!=“A” and i!=“E” and i!=“I” and i!=“O” and i!=“U” and i!=“a” and i!=“e” and i!=“i” and i!=“o” and i!=“u”:
new_word = new_word + i

print new_word


Anti_vowel
#2

Python gives us a very powerful operator, in, that can erase all that code bloat in your if statement. i is usually used to represent an index, not a value. Suggest use a more semantic name, such as letter or char.

for letter in text:
    if letter not in 'aeiouAEIOU':
        new_word += letter

#3

Your code is correct, the problem is that the challenge asked you to Return the new string not print it. Also I’d like to point out there is an easier way to write your letter exceptions. Rather than writing an if statement with a ‘not equals’ for each and every letter, upper and lowercase, you can just use a list.

vowels= ['a', 'e', 'i', 'o', 'u']

and to deal with your pesky uppercase letters you can just use the .lower() built-in function.

for i in word:
    if i.lower() in vowels:

and then lastly to remove it there are a few options but .remove() is the most straightforward
text.remove(i)

There’s a whole bunch of ways to solve these challenges. It’s always good to figure out more than one way to solve a problem. Can come in quite handy later on.


#4

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