8/15 my solution to anti_vowel


#1

I thought it would be easiest to incorporate a list as part of the solution. Below is the code:

 def anti_vowel(text):
        anti = ""
        vowels = ['A','E','I','O','U','a','e','i','o','u']
        for char in text:
            if char not in vowels:
                anti = anti + char
        return anti

#2

@yahoosmith
Looks good, while you can search for both capital and lower case you can just make them all lower case when you import the text with the lower() builtin string method.

word = 'WORD'
l_word = word.lower()

Also you are correct using a list is a good way to do this but you can also do it like this.

word = 'WORD'
 def anti_vowel(text):
        anti = ""
        for char in text.lower():
            if char not in 'aeiou':
                anti += char
        return anti
anti_vowel(word)

Note that there is nothing wrong with your code just as you get better you would not repeat using code or make containers to hold data if you don't need to.

Also here is a one liner for you to look at.

def anti_vowel(text):
    return ''.join([letter for letter in list(text.lower()) if letter not in 'aeiou'])

#3

awesome, thanks for the feedback!


#4

Here was my solution, though a little longer than some of the above. I new to the course and I was not aware of the ' not in' approach.

def anti_vowel(text):
    wrd = ""
    for letter in text:
        if letter.lower() == "a" \
        or letter.lower() == "e" \
        or letter.lower() == "i" \
        or letter.lower() == "o" \
        or letter.lower() == "u":
            wrd = wrd
        else:
            wrd = wrd + letter
    return wrd

I also tried the alternative approach above and found that the following from YahooSMith works if you put the .lower() within the if statement:

word = 'WORD'
def anti_vowel(text):
        anti = ""
        for char in text:
            if char.lower() not in 'aeiou':  # added .lower() here
                anti += char
        return anti
anti_vowel(word)

Learned a lot on this exercise. Thanks to all.


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

#6

Is there a way to remove every vowel in the text?
I see your solution is to add up every item that is NOT vowel. It's brilliant.

My original method was to remove every vowel in the text. And I failed to get the correct answer.


#7

Your simple solution to this problem makes me want to give up programming. Only when I see other people's code does the the "Ahhh. I get it" moment ever occur. Thanks for the solution!


#8

My solution

def anti_vowel(text):
newList=[]
vowel="aeiouAEIOU"

for t in range(0,len(text)):
    if text[t] not in vowel:
        newList.append(text[t])

return "".join(newList)

print anti_vowel("Hey You!")


#9

A bit lengthy, but it gets the job done. Here is my solution:

# remove all vowels from a given text:
def anti_vowel(text):
    text = str (text)
    newstr = ""
    for letter in text:
        if letter in "aeiouAEIOU":
            newstr = newstr + ""
        else:
            newstr = newstr + letter
    else:
        return newstr
            
print anti_vowel ("abelone")

#10
def anti_vowel(text):
    no_vowel = ""
    for char in text:
        if char.lower() not in "aeiou":
            no_vowel += char
    
    return no_vowel

#11

Try this

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


#12
def anti_vowel(text):
    vowels = 'aouei'
    return ''.join(a for a in text if a.lower() not in vowels)

#13

I feel so stupid when I saw you guys' solutions. Here's mine:

def anti_vowel(text):
i = 0
s = ''
while i < len(text):
    n = 0
    for c in "AEIOUaeiou":
        if text[i] != c:
            n += 1
    if n == 10:
        s += text[i]
    i += 1
return s

#14

I was looking for the simplest solution and your code helped out a lot. Thanks erikvec.


#15

I am a noob.... I liked your method, however had to move the .lower for it to work.

def anti_vowel(text):
    voweless = ""
    for x in text:
        if x.lower() not in 'aeiou':
            voweless += x
    return voweless

#16

While my solution is clearly not the most elegant, I can't figure out why it doesn't work. Error message states that it returns "Hy lk Words!" when it should return "Hy lk Wrds!". Not sure why it's only reaching the first two Os. Any suggestions?

def anti_vowel(text):
    text_list = []
    for each in text:
        text_list.append(each)
    for item in text_list:
        if item.lower() == "o" or item.lower() == "e" or item.lower() == "i" or item.lower() == "u":
            text_list.remove(item)
    return "".join(text_list)

#17

I didn't see a reply to your question, so apologies if you already got a solution for removing vowels. Here is mine.

def anti_vowel(text):
    vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']     #list of the vowels
    text_to_list = list(text)   #makes the input string into a list
    
    for i in text:  #iterating through the initial string
        for v in vowels:    #iterating through the list of vowels
            if i == v:  #checking whether there are any vowels in the string
                text_to_list.remove(i)  #removing the vowels from the list
    
    return "".join(text_to_list)    #reformating the list back into string and joining the items

word = raw_input("Enter your text: ")   #allowing the user to input text
print ("Your text without the vowels: %s") % anti_vowel(word)   #returning the text without the vowels

#18

The hint is also helpful, giving some code that can save some coding;

Hint
To check to see if c is a vowel, you can do: c in "aeiouAEIOU".


#19

what's wrong in my code??

def anti_vowel(text):
for char in text:
ch=char.islower()
if ch=="a" or ch="e" or ch="i" or ch="o" ch="u":
return ""
else:
return ch


#21

hey zeziba, could you explain how the one line works please?