Anti_vowel, why it works outside the loop?


#1



This code below prints: "Crlo is the boy in sun" Exactly what it should produce.


b = "a"      
text = "Carlo is the boy in sun"
print text.replace(b,"")

so I wanted to use this logic to extend the "b" list and place it in a loop. like so:

def no_vowel(text):
    vowels = ("aei")
    no_vowel_text = ("")
    for i in text:
        no_vowel_text += i
    for i in vowels:
        no_vowel_text.replace(i,"")
    print no_vowel_text

this produces the whole sentence without replacing anything.

Can you point out the direction?

Thanks


#2

what does this code do:

for i in text:
        no_vowel_text += i

text is already a string, so why loop over and append it to a new string? We talked about strings being immutable in python, right?

you do the replace() action, but you never store the result of this replace anywhere.


#3

ok I agree, this is useless.

I tried this to append to a new list but it doesn't work:

> new_phrase.append(no_vowel_text.replace(i,""))


#4

this also means you made a empty list somewhere, if you make changes, please post your full code.


#5

the empty list is the no_vowel_text

def no_vowel(text):
    vowels = ("aei")
    no_vowel_text = ("")
    for i in text:
        no_vowel_text += i
    for i in vowels:
        no_vowel_text.replace(i,"")
    print no_vowel_text

#6

first, the exercise expects the function to be named anti_vowel, not no_vowel

Maybe add a function call so you can add print statement to see what is actually happening?

we just determined that this lines:

    for i in text:
        no_vowel_text += i

are not doing much?

this means we have to make a small change here:

for i in vowels:
        no_vowel_text.replace(i,"")

now, no_vowel_text no longer exist. we can use text instead since this contains our string.

You do the replacement, but you don't store the result of the substitution anywhere. Maybe we need to store the result substitution in a variable?

What do you think, do we need a new variable to store this substitution or should we update/re-assign an existing variable?


#7

haha ! That was fun and much shorter then expected:

text = "Carlo is the boy in sun"
vowel = "aeiouAEIOU"

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

Thank you!!!!!


#8

why do you declare vowel twice? seems a bit unnecessary. also, when declaring vowel, no need to use parentheses

But nicely done, you figured it out :slight_smile: Hope you learned something :slight_smile:


#9

Yes I noticed this right after i posted...


#12