10. Censor


#1

Hi, I need some help revising my code for censor. Here is my code:
def censor(text, word):
words = text.split(" ")
censored = []
for item in words:
if item == word:
censored.append("*" * len(item))
else:
censored.append(word)
return censored

The error message is: Your function fails on censor("hey hey hey","hey"). It returns "['', '', '']" when it should return " *** ***"

My guess was that it wants censored to be a string, but when I change it to a string it still does not work. In other words, my code works but returns brackets instead of quotations because it is a list and it will not work if I change it to a string.
Please Help!
Thanks!


#2

Did you mean to write, item? That line is appending the word that supposed to be censored from the text.

At the end, return a join of the list:

return " ".join(censored)

That will give back a string.


#3

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