Censor - Only one occurence replaced


#1

I want to replace all words "You" by ***. But I only the last occurence is replaced and I don't understand why. How do I make sure all occurence are replaced?

Here is the result I get :

Enter a sentence : You, Where Are You, You Funny
Choose a word from your sentence you want to erase : You
[u'you,', u'where', u'are', u'you,', u'you', u'funny']
[u'you,', u'where', u'are', u'you,', '***', u'funny']
you, where are you, *** funny
None

Here is my code :


result = []

def censor(text, word):
    texta = text.lower()
    worda = word.lower()
    lista = texta.split()
    
    for i in lista:
        if i == worda:
            i = "*" * len(worda)
            result.append(i)
        else:
            result.append(i)
    new_list = " ".join(result)
    print lista
    print result
    print new_list
    return new_list
    
        
usertext = raw_input("Enter a sentence : ")
userword = raw_input("Choose a word from your sentence you want to erase : ")

censor(usertext, userword)


#3

the problem is in the split, if you print i:

you,
where
are
you,
you
funny

you can see some instances of you have a , at the end. you, does not equal you

if your sentence would be:

You , Where Are You , You Funny

everything would be fine

a possible solution would be to split the string at multiply places, but you need regex for it, you can find an example here


#4

Thank for that! I haven't thought about it!

I tried it with a new code:

usertext = "hey hey hey"
userword = "hey"
result = []

def censor(text, word):
    texta = text.lower()
    worda = word.lower()
    lista = texta.split()
    
    for i in lista:
        if i == worda:
            i = "*" * len(worda)
            result.append(i)
        else:
            result.append(i)
    new_list = " ".join(result)
    print lista
    print result
    print new_list
    return new_list
    
        
#usertext = raw_input("Enter a sentence : ")
#userword = raw_input("Choose a word from your sentence you want to erase : ")

censor(usertext, userword)

This time, the code seems to work well :

['hey', 'hey', 'hey']
['***', '***', '***']
*** *** ***
None

But i still have an message of error :

Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** the password is * *** *** ***" when it should return " *** ***".


#5

That is an interesting little error that stems from result = [] being outside of the function. Because result = [] is used by the function and only by the function, it should really be placed inside the function. If you move it inside then that will fix your current error.


#6

Thank you, it fixed the problem!


#7

the exercise only calls the function (to validate your code), not the whole script. Which means if result is defined outside the function, the array isn't cleared each time the function is called


#8

Thank you very much for this additionnal information. It make thing very clear.


#9

def censor(text,word):
    text=text.replace(word,len(word)*"*")
    return text

You can also use this.

.replace can also be used in the anti_vowel() challenge.

From the documentation:

string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.