Anti vowels


#1

Hi!
I am getting trouble in the exercise, I need help!
so the exercise is to remove from a string all the vowel.
that’s what I tried to do:

def anti_vowel(text):
  result = []
  for char in text:
    result.append(char)
    vowel = ["a", "e", "i", "o", "u"]
    for i in result:
      for w in vowel:
        while i != w:
          total = ""
          total_word = total + i
  return total_word
print anti_vowel("Hey look Words!")

As you can see, I tried to make the string into a list of characters and then I made a list with all the vowels and tried to compare it with it.
I saw on the internet other ways to do it and I tried to this my way.
Can my way work? What is the problem with it? can you explain a different way?
Thank you!


#2

there are several problems with your code.

The biggest problem is at the moment here:

while i != w:

its an infinity loop, and it shouldn’t be a loop

please run this code:

def anti_vowel(text):
  result = []
  for char in text:
    result.append(char)
    vowel = ["a", "e", "i", "o", "u"]
    for i in result:
        print i

print anti_vowel("Hey look Words!")

look how many times you are loop text, way more then you should.

There remain issues, but these are the two you should prioritize on fixing.


#3

Hi!
Thank you for the answer! I thought that while loop will end at the end of the list.
so the for loop will end at the end of the list?
With what you wrote it just print in random order the letters., and that still not good.
I thought to do:

def anti_vowel(text):
  result = []
  for char in text:
    result.append(char)
    vowel = ["a", "e", "i", "o", "u"]
    for i in result:
      for w in vowel:
        if i == w:
          result.remove(i)
  return result
print anti_vowel("Hey look Words!")

And it printed:

[‘H’, ‘y’, ’ ', ‘l’, ‘k’, ’ ', ‘W’, ‘r’, ‘d’, ‘s’, ‘!’]

I almost there I just need to get it from a list to a string and sum it somehow.
Can you help?


#4

no, the while loop run as long i didn’t equal w (condition evaluate to true), and given i and w where not changing, you had an infinity loop

you can create a empty string and append/add to this empty string using a loop, or use a built-in method which i think has been taught

but your solution could use some optimization, if we add a counter to see how many loop iterations your program makes:

def anti_vowel(text):
  counter = 0
  result = []
  for char in text:
    result.append(char)
    vowel = ["a", "e", "i", "o", "u"]
    for i in result:
      for w in vowel:
        counter += 1
        if i == w:
          result.remove(i)
  print counter
  return result
print anti_vowel("Hey look Words!")

its a staggering amount!


#5

Hi!
I really appreciate your help!
I looked and how many loops it makes and it 430…
how can I optimize it?

And I looked online and there is something called " ".join
I tried it in my function and it did what I looked for:

def anti_vowel(text):
  result = []
  for char in text:
    result.append(char)
    vowel = ["a", "e", "i", "o", "u"]
    for i in result:
      for w in vowel:
        if i == w:
          result.remove(i)
  return ' '.join(result)
print anti_vowel("Hey look Words!")

The print is:
H y l k W r d s !

The Codecamdey says it still wrong, can you help me with that?

Thank you!


#6

here:

return ' '.join(result)

the character between the apostrophes (currently a space) is the glue used, should that be a space here?

each time you append a character to result, you are entirely looping over result, checking for vowels to be removed. It would be more efficient to construct results before looping over it

however, this will create a new problem (given remove is not a good approach for this problem), you overcame this problem in your current code by using so many loop iterations


#7

Thank you for the help !
I really need to practice a lot to get how to use each code and where to put it.