Anti_vowel, please Help!


#1

Please, Can someone tell me what i’m doing wrong?

def anti_vowel(text):
  list_of_char = []
  for char in text:
    list_of_char.append(char)
  final_text = ""
  vowels_list = ["A","a","E","e","I","i","O","o","U","u"]
  for char in list_of_char:
    for vowel in vowels_list:
      if char == vowel:
        list_of_char.remove(char)      
    final_text = final_text + char
  return final_text
    
print anti_vowel("Hey look Words!")

the result is:
"He lok Wods!"

#2

Using remove, you are really complicated things for yourself.

you could do:

for vowel in vowels_list:

else:
   # append to string

when a loop breaks, the else clause won’t run. The else does run when the loop condition becomes false (when loop finishes running)


#3

putting the else, results in the same output had before: ;-/

def anti_vowel(text):
  final_text = ""
  list_of_char = []
  vowels_list = ["A","a","E","e","I","i","O","o","U","u"]
  for char in text:
    list_of_char.append(char)  
  for char in list_of_char:
    for vowel in vowels_list:
      if char == vowel:
        list_of_char.remove(char)      
    else:  
      final_text = final_text + char
  return final_text
   
print anti_vowel("Hey look Words!")

Output: He lok Wods!


#4

but you shouldn’t use .remove(), you should break the loop if char equals vowel. Using remove really complicates matters.

you could also use a boolean value if you prefer:

for char in list_of_char:
    is_vowel = False
    for vowel in vowels_list:

then if vowel equals char, set is_vowel to true. Then before appending, check the value of is_vowel

we could go into detail why remove is a really difficult approach if you want


#5

I got it, breaking the loop, worked fine, but I will use a boolean value, its looks the better thing to do.

Thanks a lot for replaying Stetim94, and yes, if you have time, we can go into details why remove() is a bad choice for that.
I’m brazilian and my english is terrible, but i will enffort myself to understand everything you teach me :slight_smile:


#6

you need to understand that lists can’t have empty places/spaces. So lets say we have to following list:

l   o   l
0   1   2

so a list with 3 items, and below i show the indexes. when we do:

.remove(o)

then the list becomes:

l   l
0   1

the o at index 1 got removed, so everything to the right of the removed item shifts one spot/place to the left

This makes it really difficult to use for anti_vowel, we can see this when inserting some print statements:

def anti_vowel(text):
  final_text = ""
  list_of_char = []
  vowels_list = ["A","a","E","e","I","i","O","o","U","u"]
  for char in text:
    list_of_char.append(char)  
  for char in list_of_char:
    for vowel in vowels_list:
      if char == vowel:
        print char,
        list_of_char.remove(char)      
    else:  
      print char
      final_text = final_text + char
  return final_text
   
print anti_vowel("Hey look Words!")

in inserted two print statement, the first one has a comma, which will cause the next print statement to print on the same line.

after removing a vowel, char still holds the vowel value. We remove it from the list, sure. But that won’t prevent it from being added to final_text

and the character after the removed character never gets added to final_text, because of the index shift i explained earlier in this answer

to overcome all this problems, is a challenge.


#7

i understand Stetim, the big problem is the char variable still holds the vowel after remove(), having this in mind. can i simple set char = ‘’ in that case and add in final_text?


#8

NO, i cant LOL

because the index still shifts and the next letter “pass” to left,


#9

the for loop assigns value from the list to the loop iterator (char in your case). You remove from the list, but char already has a value by then. And simple setting it to an empty string, won’t solve the index shift problem as you noticed. Which is why advised from the beginning not to use .remove()


#10

I appreciate for your time Stetim, for the didactic lesson…i will keep that dificult ussing .remove() in consideration after this, thanks a lot :raised_hands:


#11

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