Anti_vowel, text not defined?


#1



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


Code returns NameError:'text' is not defined.


I don't understand what is wrong with my code, text is the input of the function anti_vowel and in other exercises we haven't defined the input of the function.


def anti_vowel(text):
     vowels = ['a','e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
     no_vowels = " "
     for c in text:
          for vowel in vowels:
               if c == vowels:
                    break
               else:
                      no_vowels += c


#3

I have checked your code and it returns anything but NameError:'text' is not defined)) Its OK with 'text'! But there is problem with nested for loop: it seems to be

for vowel in vowels:
    if c == vowel:
    break
else:
    no_vowels += c

'else' should be after 'for', not after 'if'. And don`t forget to return no_vowels :slight_smile:
When you define no_vowels use "" instead of " ", because it prints an extra space

Full code:

def anti_vowel(text):
    vowels = ['a','e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
    no_vowels = ""
    for c in text:
        for vowel in vowels:
            if c == vowel:
                break
        else:
            no_vowels += c
    return no_vowels

#4

Thank you, it worked. Your post was very helpful.


#5

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