Anti_wovel


#1

Hi, I tried different methods but they do not work:

the task is: Define a function called anti_vowel that takes one string, text, as input and returns the text with all of the vowels removed.

For example: anti_vowel(“Hey You!”) should return “Hy Y!”. Don’t count Y as a vowel. Make sure to remove lowercase and uppercase vowels.



def anti_vowel(text):
  vowel = ['a','e','i','o','u']
  text.lower()
  novowel =""
  for i in text:
      if i != vowel[""]:
         nowovel = nowovel + i
  return nowovel


it gaves me this error message:

Does your anti_vowel function take exactly one argument (a string)? Your code threw a “list indices must be integers, not str” error.

so I did not know why he gaves me an error so I tried this:


def anti_vowel(text):
  vowel = "aeiou"
  text.lower()
  novowel =""
  for i in text:
      if i != vowel[""]:
         nowovel = nowovel + i
  return nowovel



with the same magnificent error alert.

so I tried a different approach, trasform the beloved string in a list:


def anti_vowel(text):
  text.lower()
  l = list(text)
  vowel = ['a','e','i','o','u']
  for i in text:
    if i == vowel['']:
      del(text[i])
  s = "".join(text)
  return s
  print s

anti_vowel("salvo")



with this error:

Does your anti_vowel function take exactly one argument (a string)? Your code threw a “list indices must be integers, not str” error.

and then on the right side of the screen is appearing this:

Traceback (most recent call last):
File “python”, line 12, in
File “python”, line 6, in anti_vowel
TypeError: list indices must be integers, not str

thank you for your help


#2

don’t go for the del approach, the problem is here:

if i == vowel['']:

you attempt to access list (vowel) by index, but for this you use a string (''), which won’t work, indexes have to be integer

you would have to loop over vowel to check if i is not in vowel list, you could also use the in keyword


#3

@stetitm94 thank you for the hint

i tried in this way and it is working:

def anti_vowel(text):
vowel = “aeiouAEIOU"
novowel =”"
for i in text:
if i in vowel:
novowel = novowel
else:
novowel = novowel + i
return novowel

but because I want to understand better I have some questions:

  1. I tried with

vowel = "aeiou"
text.lower()

and it did not work, telling me that it doesn’t work with caps lock letter. why?

  1. In this case I used:

for i in text:
if i in vowel:
novowel = novowel
else:
novowel = novowel + i

but to reduce code lines, there is a way to write for i in NOT wovel? I mean, a way to make it not considering some elements?

  1. and last question, you wrote about the del approach, there is a way to access the list elements and compare with i and after remove?

thank you very much again


#4

The problem is, once you passed the exercise, you can’t test the code again. (not against the validation anyway)

well, .lower() returns the lower string so you need to store it in a variable:

text = text.lower()

but if you want keep case-sensitivity, this is not the way to go

not is a keyword, which you learned. Which can be combined with in but then its not in, the not keyword has to before what you want to reverse (from false to true and vice versa), given in returns True if element is present, we want to reverse this to shorten a few lines

well, del presents additional challenges. Prove of concept:

text= ["b","o","o","k"]
vowel = ['a','e','i','o','u']
for i in range(len(text)):
    if text[i] in vowel:
      del text[i]

print text

for starters, see how much more code is needed? using range() and so on? And it doesn’t even work, given we remove items we get an index error

so we could use .remove() instead:

text= ["b","o","o","k"]
vowel = ['a','e','i','o','u']
for i in text:
    if i in vowel:
      text.remove(i)

print text

but as you can see, this presents us with yet another problem, it doesn’t properly remove successive vowels because of index shifts (everything to the right of the removed item shifts one index to the left, given lists can’t have empty indexes while the loop moves to the next index causing successive vowels to be skipped)

Now, its possible to use del or remove(), don’t get me wrong, but as you can see, some additional challenges


#5

@stetim94 Thank you very much for your help,

about .lower() I got that if I want to use I have to store in a variable.

about the use of not I have still some doubt about it is usage.as example:

def anti_vowel(text):
  vowel = "aeiouAEIOU"
  novowel =""
  for i in text:
      if i not in vowel:
         novowel = novowel + i 
       
  return novowel

it will not work?


#6

not in is working fine, i threw some test cases to it and they handled well, there is no problem with the code


#7

ok thank you very much,

sorry I am a beginner in python. so, if I understood well the code with “not in” I wrote in the last post it will work too, right?

about the del approach I got it doesn’t work but It did not figure out why.

Just another question about .remove(),

text= [“b”,“o”,“o”,“k”]
vowel = [‘a’,‘e’,‘i’,‘o’,‘u’]
for i in text:
if i in vowel:
text.remove(i)

print text

if I understood well this code you wrote is not properly working beacuse as you told me there is index shift. To avoid that it skips vowel I have to make it working I have to return text? right?

like this?

text= [“b”,“o”,“o”,“k”]
vowel = [‘a’,‘e’,‘i’,‘o’,‘u’]
for i in text:
if i in vowel:
text.remove(i)
return text

print text


#8

yes, you can always add a couple of function calls to test

Well, an index error is what we get when we try access an index that doesn’t exist:

x=[1,2,3]
print x[5] # index error

so clearly the way del removes causes problems

no, return will just end the function, so only the very first vowel gets removed, then the function ends


#9

ok, than you very much, I understood

no, return will just end the function, so only the very first vowel gets removed, then the function ends

so in this case

text= [“b”,“o”,“o”,“k”]
vowel = [‘a’,‘e’,‘i’,‘o’,‘u’]
for i in text:
if i in vowel:
text.remove(i)
return text

print text

we get printed “bok”, thene there is a way to correct and make it remove also the other vowel? how it should be if we are using the remove method?


#10

that is the challenge, go think about it. If i spoil you for it, there is no fun to it. You can always come back later to this problem


#11

right :slight_smile:

so if I understand well, if we use remove method awe get “bok”, so the best idea should be a system that take back again the “text” and makes do another cycle until all vowels are removed.

so I thought that the best way to mantaina cycle until the condition is satisfyed is using while.right?

so I was think something like this:

text= [“b”,“o”,“o”,“k”]
vowel = [‘a’,‘e’,‘i’,‘o’,‘u’]
while i in text == i in vowel:
for i in text:
if i in vowel:
text.remove(i)
return text

print text

but in my opinion for the “book” it will print “book” because the first element is b, right? or it will continue until any of i in text is equal to i in vowel?


#12

But then there are still extreme cases (a lot of successive vowels) which might cause problems.

Maintaining a cycle would be a very expensive operation, far from ideal. It seems you don’t have the skill level (understanding, problem solving) to tackle this problem, you should let it rest for now, and come back later

you can attempt it on your own, but i think i provided enough assistance for now


#13

thank you very much for your support,

I know I am beginner

just my last question: does the argument will be faced in the next lessons in the course? or in case do you suggest something to improve my skill (other code academy course etc…)?


#14

which argument? i don’t understand the question

There is always room to improve your programming skills, to improve problem solving skills go through codewars


#15

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