anti_vowel code help


#1

def anti_vowel(x):
a = []
c = ""
for n in x:
a.append(n)
for b in a:
if b in "aeiouAEIOU" :
a.remove(b)
for d in a:
c = c + d
return c

print anti_vowel("look")

This is my code. It returns lok instead of look. Oddly enough if I input "Hey look Words!" it will return Hy lk Words!"
I don't know why it skips some vowels.


#2

Also imagine that the indentations are correct


#3

@antonio_42

Well, the reason why your code does not work so well is because of how str.remove() works. I will not get into it because there are a couple of reason but it only takes out the lead most item that matches.

Moving on, how to actually build this in a way that works.

  1. First you have to iterate over your text.
  2. Second match if the iteration is in our vowel list/str
  3. Third only get the items that are not in the vowels

Sounds easy enough right?

Any time you have a problem you need to solve programmatically always write out the steps you need to complete in order to succeed. These are the same steps you will take when programming your problem. Make sense?

Let's get to it:

data = 'A string of random junk'
select_data = []
# Step One
for item in data:
    # Step Two
    if item.lower() not in 'aeiou': # This automagically iterates through our string.
        # Step three
        select_data.append(item)
print(select_data)

As you can see quickly I have all three steps necessary to complete this task programmed there. Nothing extra, this make it easier to read and understand, and that is why it is important to understand the steps you need to take in order to program your problem.

Having said all that, good luck, and any more questions feel free to ask.


#4

Rather we can simply create another string concatenating characters that are not vowel as follows:

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


#5

For me the solution had to do with removing the vowels, so:

def anti_vowel(t):
tf=t
x=['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
for i in x:
tf = tf.replace(i, "")
return tf