Censor - Different approach


> def censor(text,word):
>     if word in text:
>         word_stars = ""
>         word_stars = len(word) * "*"
>     text.replace(word, word_stars)
>     print text
> censor("this hack is wack hack", "hack") 

I tried the above code, but i get the following error message

Oops, try again. Your function fails on censor(“hey hey hey”,“hey”). It returns “None” when it should return “*** *** ***”.

Any help would be appreciated, thank you in advance.


That should be a return statement.


Nope, tried that. Not working. I used print to check the function works correctly. But i get an error message.


Can you think of another approach besides replace()? If we do not understand its usage, it is not much help. From what I see above, that is the case here. It is all fine and good to drum up an elegant solution, however, how much is learned or practiced?

The real aim of this module is to give us a chance to explore what we have learned and put it to use without reaching for the Python tool box.

Just to put this to rest and set it aside…

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

Now work out some other solutions that do not use built-ins. You’ll be glad you did.


Thank you, it works this way. I will try the “hard way” as well. :laughing:


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