Python Censor Activity


I am working on the Censor activity in the unit Practice Makes Perfect.

This is my code:
def censor(text,word):
text = text.split()
new_text = ""
for item in text:
if word == item:
for i in item:
new_text += "*"
new_text += " "
new_text += "%s " % item
return new_text

print censor(“hey holla hey hey”,“hey”)

The code works perfectly when I test it but the activity will not complete, it is giving me this error:
Your function fails on censor(“hey hey hey”,“hey”). It returns " " when it should return " ".

I do not know what I am doing wrong since the code is working when I test it but the activity won’t mark as complete.

Apparently the code formatting did not copy over, here is the code in paste bin:


you can use format to make code show:

anyway, the problem with your solution seem to be the additional space at the end of the sentence, so for:

censor("hey hey hey","hey")

the output is:


i replaced the spaces with underscores, as you can see your code gives an additional _ at the end of your string


Thx, that was the problem. I added new_text = new_text[:-1] before the return command to fix the problem.