8/15 anti_vowel


Could You guys check what is wrong with this code? driving me mad…

def anti_vowel(text):
  for c in text:
    for i in "aeiouAEIOU":
      if i == c:
        text = text.replace("i","")
        text = text
    return text



text = text.replace("i","")

why do you use "i"? this always a string with the letter i, you should use the variable i, which contains vowels thanks to your for loop

your else clause isn’t doing much, nothing in fact, you can just get rid of it


Also, you’re doing many more replacements than required, if you read what it does, which you certainly should if you’re using it, then you’ll see that it replaces all occurrences that it finds.
The replace function also does nearly the whole job for you. It’s definitely a good idea to figure out how to use it - but it may also be a good idea to make a second version where you push letters around yourself to get the desired result. Try to avoid doing lots of copies of the whole string, that can turn out incredibly expensive for long text - you should essentially only loop through the whole string once or twice and process one letter at a time. (Keep in mind that some functions will have loops themselves, so just because you don’t have a for-loop doesn’t mean there isn’t a loop)


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