8. Anti Vowel


#1



Stuck on Anti_Vowel. I can see some others peoples answers on here but just dont understand why this is not working and want to understand it.


The error shows ---- Your function fails on anti_vowel("Hey look Words!"). It returns "Hey lk Wrds!" when it should return "Hy lk Wrds!". ----- Cant undestand why it removes the other vowels but not the first one?


def anti_vowel(text):
    vowel = ["a","A","e","E","i","I","o","O","u","U"]
    for v in vowel:
        if v in text:
            replacing = text.replace(v,"")
            
    return replacing


#2

The issue is that you are modifying replacing and not the text, so you change it on time but the next loop text keeps being the first text.

I debug your code like this

def anti_vowel(text):
    vowel = ["a","A","e","E","i","I","o","O","u","U"]
    for v in vowel:
        if v in text:
            print v
            replacing = text.replace(v,"")
            print replacing
            
    return replacing
    
print anti_vowel("tentaculo")

#3

Here the solution

def anti_vowel(text):
    vowel = ["a","A","e","E","i","I","o","O","u","U"]
    replacing = text
    for v in vowel:
        if v in replacing:
            print v
            replacing = replacing.replace(v,"")
            print replacing
            
    return replacing

#4

Coding is amazing. There are many good answers. I took a different approach.

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



text = "Hey look Words!"
print anti_vowel(text)

GLTA


#5

Here is my code:

def anti_vowel(text):
new = []
vowels = ["e","u","i","o","a"]
for char in text:
if char.lower() not in vowels:
new.append(char)
return "".join(new)


#6

never mind, made a mistake


#7

def anti_vowel(text):
replacement=text
for v in 'AaEeIiOoUu':
if v in replacement:
replacement=replacement.replace(v,"")
print replacement
return replacement


#8

def anti_vowel(x):
list1 = list(x)
list2 = []
for i in list1:
#return list2.append(list1[n])
Ans = i in "aeiouAEIOU"
if Ans == True:
list1.remove(i)
#print list1
else:
list2.append(i)
#print list2
return "".join(list2)
print anti_vowel("Hey look Words")

Hijacking this post to find some assistance/explanation. So the above code isn't the most efficient, but i wrote it just messing around with different ways to accomplish this task. For some reason when this code is ran, the result is "H lk Wds"

The hashed out print comments were simple inserted as i was trying to understand why it was ignoring the 'y'

Also, if anyone cares, the following worked for me:
def anti_vowel(x):
list1 = list(x)
list2 = []
for i in list1:
#return list2.append(list1[n])
Ans = i in "aeiouAEIOU"
if Ans == False:
list2.append(i)
return "".join(list2)
print anti_vowel("Hey look Words")


#9

I also took this approach I did not deem it necessary to create a list. I can see where a list could be more useful in this however. If we were not dealing with individual characters but perhaps wanted to parse through an entire document and pick out, say the word the and replace it with another word. Then I could see where making a list for this example is useful.

Can someone tell me which way is better from a logical standpoint? I am struggling with the logic in this course. Can anyone recommend a book on the logic behind programming? Or just continue with lessons and I will learn logic in the process.


#10

def anti_vowel(text):
result=''
vowels="aeiouAEIOU"
for char in text:
if char not in vowels:
result+=char
return result

print anti_vowel('"Hey You!')


#11

I approached this without using a list. Trying to figure out why my code works in my Terminal but Codecademy gives me this error:
Oops, try again. Your function fails on anti_vowel("Hey look Words!"). It returns "None" when it should return "Hy lk Wrds!".

def anti_vowel(text):
vowels = "aeiouAEIOU"
for letter in text:
if letter in vowels:
print "",
else:
print letter,


#12

You did not return anything, so the output is none.

Instead you can do this:

def anti_vowel(text):
vowels = 'aeiouAEIOU'
s=""
for letter in text:
if letter not in vowels:
s += letter
return s


#13

This is because when you remove a letter from list1 in line 8, you are are modifying the list when it loops through list1 on line 4.

Hence when it removes the 2nd letter 'e' from "Hey look Words", "y" now becomes the second letter and it skips it when it looks for the third letter, which is space " ".


#14

brute force always work (below)


#15

Anyone an idea why the following works except for the last vowel in the test word?

def anti_vowel(text):
    vowels = "aeiouAEIOU"
    text = list(text)
    for char in text:
        if char in vowels:
            del(text[text.index(char)])
    text = "".join(text)
    return text

print anti_vowel("testschriftstueck")

#16

Text is a string and you can not modify string your create new ones.
To debug and see what I mean "print text" after your del (text[text...]...)... statement.


#17

-- This is my Code, i cant figure whats wrong with it.. --
vowels = ["a","e","i","o","u","A","E","I","O","U"]

def anti_vowel(text):
appened = str(text)
for i,p in (range(len.text()), text):
for n in vowels:
if p == n:
a.pop(appened[i-1])
return appened
print anti_vowel("hi")


#18

Can you explain me what are you trying to do with the i, and p in the for loop. Also the text is already a string


#19

While a letter in a string is being iterated another for loop is initiated that goes through the vowel list and checks if it is the same as the string letter if it is it doesn't add into the new word variable. after the for loops are expended the finished variable is returned.


#20

also instead of appending letters to the variable i take away vowels as they match up to vowels. i know now there is easier ways of solving this problem this is my initial approach and in my mind it should still work.