8/15 anti_vowel


#1

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","")
      else:
        text = text
    return text

#2

here:

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


#3

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)


#5

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