My code doesn't work! Please help!


#1



The exercise I'm stuck at: anti_vowel


says:

File "python", line 5
if text[t]=vowel[v]:
^
SyntaxError: invalid syntax

also,

Oops, try again. Did you create a function called anti_vowel? Your code threw a "global name 'anti_vowel' is not defined" error.


def anti_vowel(text):
    vowel=['a','e','i','o','u','A','E','I','O','U']
    for t in text:
        for v in vowel:
            if text[t]=vowel[v]:
                del text[t]
    return "".join(text)


#2

= is how you would declare something, what is the comparator to see if something equals another?


#3

After vowel=['a','e','i','o','u','A','E','I','O','U'], create a result variable and assign an empty string to it, like so: result = ''

Then, loop through text. For each character in the text, if the character is not in vowels, add the character to the result: result += character

Return the result.

Hope that helps.


#4

Thanks for suggestions guys! I worked on my code and its working now!

def anti_vowel(text):
    vowel=['a','e','i','o','u','A','E','I','O','U']
    result=[]
    for t in text:
        for v in vowel:
            if t==v:
                break
        else:
            result+=t
            
    return "".join(result)

#5

Happy for you :slight_smile:

You can also achieve the same without nested for-loop, else statement and join() method. Consider the code below.

def anti_vowel(text):
    vowels = 'aeiou'
    result = ''

    for char in text:
        if char.lower() not in vowels:
            result += char
    
    return result

Hope that helps.


#6

That's great! Thanks!


#7

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