Anti_vowel ---> but WHY !?!?


#1



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


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


it should works correctly, but WHY !!!!


def anti_vowel(text):
    vowels = ['a','e','i','o','u','A','E','I','O','U']
    l = list(text)
    word = ""
    for char in l:
        if char in vowels:
           l.remove(char)
    for x in l:
        word += x
    return word


#2

Every time you remove one item from list l, The length of list gets decreased and thus it cause to leave few vowels inside the list unremoved !

for more clarification ---


#3

Ok , I get it . & Thanks for all bro
BUT i can't Find another format that can run the code Correctly.
So , if you have an answer for that
PLEASE Help


#4

def anti_vowel(text):
    vowels = ['a','e','i','o','u','A','E','I','O','U']
    l = list(text)
    word = ""
    for char in l:
        if char in vowels:
           l.remove(char)
           for char in l: 
               if char in vowels:
                   l.remove(char)
    for x in l:
        word += x
    return word
        
print anti_vowel("Hey look Words")

i did my best & it works correctly
but i still believe that there's a better solution for that
if you have other suggestions
help me
Advance Thanks :slight_smile:


#5

def anti_vowel(text):
    word = list(text)

    #checking each letter in word
    count = 0
    while count < len(word):
        if word[count] in "aeiouAEIOU":
            word.remove(word[count])
        else:
            count += 1

    return "".join(word)

print(anti_vowel('Hey look Words!'))

The problem arises when you remove a word with a for loop. You remove a vowel at index 1 and then the next letter is now at index 1. However, the for loop is still at index 1. Then the for loop moves to index 2, but it has skipped the letter that moved into index 1. I solved with by using a while loop, and not incrementing the count when I remove a letter.

Hope this helps!


#6

def anti_vowel(text):
new_string = ""
vowels = ('a', 'e', 'i', 'o', 'u', 'A', 'I', 'E', 'O', 'U')
for v in text:
if v in vowels:
text = text.replace(v, '')
return text
print anti_vowel("Hey look Words!")

output is : Hy lk Wrds!
hope this will help :slight_smile:


#7

Thanks Bro :wink: , This is another way to do it


#8

Thank You ,
This helped me a lot

Thanks :slight_smile:


#9

Why did you create the variable

?


#10

yeah ..there is no use of that string. without that variable you can solve that code! it will not affect the code anyway.
just place # before it or just delete that line!
def anti_vowel(text):
vowels = ('a', 'e', 'i', 'o', 'u', 'A', 'I', 'E', 'O', 'U')
for v in text:
if v in vowels:
text = text.replace(v, '')
return text
print anti_vowel("Hey look Words!")

:relaxed: here we go!!


#11

I used a slightly different way: instead of removing vowels from given parameter i added non-vowels characters to a new variable. Code (purged by too many debug prints :P):

def anti_vowel(text):
    out= ""
    vowels=["a","e","i","o","u"]
    lower= text.lower()
    for char in range(len(text)):
        for v in vowels:
            if lower[char] == v:
                break
        else:
            out += text[char]
    return out
anti_vowel("aRe")

#12

def anti_vowel(text):
out_string = ''
vowels = "aeiouAEIOU"
for i in text:
if i in vowels:
continue
else:
out_string += i
return out_string
You can iterete strings like here, if char is in vowels string then continue loop to next iteration


#13

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