8. Anti-Vowel


#1

Why doesn't this code work?

def anti_vowel(text):
    string = text
    vowels = "aieouAEIOU"
    for x in vowels:
        if x in string == True:
            string = string.remove(x)
            print string
        else:
            print string

Why dosn't this code work?
#2

Well, a couple things look off. First,

for x in vowels:

You are taking each individual letter from vowels, do you mean to take that from text? (or string). Second,

if x in string == True:

x will be each letter in text, so checking if it's true doesn't really work here. You should check if each letter is not in vowels. Third, you don't want to print too early. Maybe initilialize an empty string in the beginning (example=""). Then if x is not in vowels, append x to your empty string (example += x). Once the for loop is done, then you should return your full string.


#3

well i did it in a quite different way , i made the vowels into a list, then created an empty string that will get all the letters that are not vowels gathered , then i just iterate through the string given and check if each letter is in the list , here(the ____ are indentations)
def anti_vowel(text):
____x = ["a","e","i","o","u","A","E","I","O","U"]
____nv = ""
____for z in range(len(text)):
________ch = text[z]
________if ch not in x:
____________nv = nv + ch
____return nv


#4
def anti_vowel(text):
    string = ''
    for i in text:
        if i not in "aeiouAEIOU":
            string += i
    return string

May this can help.It passed.


#5

Can someone tell me why this code is not returning correctly:

def anti_vowel(text):
vowels = ['a', 'e','i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
for c in text:
if c in vowels:
return anti_vowel(text) == text - "c"
else:
return anti_vowel(text) == text


#6

Here's my code. Works.

def anti_vowel(text):
----vowels = ["a","e","i","o","u","A","E","I","O","U"]
----new_word = ""
----for letter in text:
--------for vowel in vowels:
------------if letter == vowel:
----------------letter = ""
------------else:
----------------letter = letter
--------new_word = new_word + letter
----return new_word


#7

This is great, very simple solution.


#8

def anti_vowel(text):
lst=list(text)
n_lst=[]
word=''
for i in range(len(text)):
if lst[i]!='a' or 'A' or 'e' or 'E' or 'i' or 'I' or 'o' or 'O' or 'u' or 'U':
n_lst.append(lst[i])
else:
lst.remove[i]

    word+=n_lst[i]
return word

why is this not working?


#9

def anti_vowel(text):
lst=list(text)
n_lst=[]
vowels=['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
word=''
for i in range(len(text)):
for j in range(len(vowels)):

        if lst[i]!=vowels[j]:

            n_lst.append(lst[i])

        word+=n_lst[i]
return word

this is also not working


#10

you cant do it because string are immutable mean su cant make changes in it . So first o all you need to convert to list then only u can use such function like remove


#11

def anti_vowel(text):
string = text
vowels = "aieouAEIOU"
for x in vowels:
if x in string :
string=string.replace(x,'')
return (string)
you can use replace instead of remove it works...


#12

You need to create a new variable that is returned in the function (e.g. output) instead of using the function handle (anti_vowel(text)). It's easier to do that and not violate syntax than to "loop through the function" in a for loop.


#13

can you explain how does that piece of code work line by line


#14

I can't believe I didn't see this... was stuck forever. THNX


#15

Maybe not line by line, but here's the idea:
Once you've defined the function, it creates an empty string (called 'string' which is confusing and a poor choice of name for a variable, even though it is legal) which will be used to store the output/return of the function.
The for loop moves through the string 'text' and examines if it is a vowel. If it IS NOT a vowel it adds it to 'string'. When the for loop is complete, it returns.

I found the lesson from this particular solution to be very good, as it makes you ask the right questions: What do you want? Not vowels. OK, let me think about how to collect the 'not vowels'.


#19

Thanks This helped me a lot @penghao