Your code looks a bit off--it threw a "'list' object has no attribute 'replace'" error. Check the Hint if you need help!

I can't understand where is mistake. Does anyone can help me?

Replace this line with your code. 
def censor(text, word):
    text = text.split()
    new_text = ''
    for char in text:
        if char == word:
            new_text = text.replace(char, ('*' * len(word)))
            print new_text
    return new_text
censor('Hellow World', 'Word')


replace() will replace all instances, it is kind of boring, all you need to do is:

text.replace(word,len(word)* '*')

kind of dull. two more interesting solution would be to either to create new_text as empty list (not as string), then append censored/uncensored words to it with a if/else statement.

or use range() in your for loop, this will give you indexes which you can use to manipulate text (to replace the words that require censoring)

in both cases join the list into a string with " ".join() and return it


Thanks for your advice. I'll try.


I've changed my code and it works:

def censor(text, word):
censored_text = ''
if word in text:
censored_text = text.replace(word, ('*' * len(word)))
print censored_text
return censored_text
censor('Hello world', 'world')


but this solution is dull? look, this is all you need:

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

using built in functions like replace takes away the fun. If you have time, try one of the other suggested approaches


call your function with this:
print censor("this hack is wack hack", "is")

You will see that your solution is not correct.


based on how replace works, that make sense, good point

but the exercise doesn't present this case. But yes, this does require attention.


you're right. Code works incorrectly.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.