8. anti vowel


#1



Hello,

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


I've seen lots of solutions like "using a nested for loop" or using "Filter". It makes sense and seems clear but I really don't understand why my code is not working. For example del(t[i]) is wrong? or it is all because of the wrong place of "return"
or is it completely nonsense and am I thinking all wrong :smiley:?

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


def anti_vowel(text):
    vowels = ['a','i','u','e','o','A','I','U','E','O']
    w=''
    t=str(text) #if I don't do it, it is giving me str int problem
    while vowels: #By this I want to ensure that the code runs until it look
                  #for all the vowels. I am not sure it is correct or not.
        for i in range(0,len(text)):
            if t[i]!=vowels:
                w+=t[i]
                i+=1
            else:
                del(t[i]) 
                i+=1
        return t #by this it is directly giving "hey look words".
                 #why it doesn't use the loop? I tried to put the 
                 #return bottom alignment of while also but it didn't work either 
                 #also I tried to put break but it didn't work ofcourse


#2

while vowels: #By this I want to ensure that the code runs until it look
                  #for all the vowels. I am not sure it is correct or not.

Why a while loop? A while loop runs until you make the condition false, i would use a for loop to loop over all your vowels, a for loop loops until it had all items in your vowels list

t=str(text) #if I don't do it, it is giving me str int problem

shouldn't be necessary once you fixed the issues of your code

the biggest problem is in this line:

if t[i]!=vowels:

this will always be true, t[i] is a single letter where vowels is a list

I would actually reverse the whole thing:

loop over text, then loop over vowels to see if the letter is a vowel or a constant

using a for loop, you get actually access to the vowels, instead of the whole list.

You can of course also use in, a lot easier (if you don't want two loops), which checks if letter is in vowels list


#3

thanks a lot now I understand my mistakes :slight_smile:


#4

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