Python Censor Activity


#1

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 += " "
else:
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.

EDIT:
Apparently the code formatting did not copy over, here is the code in paste bin:
https://pastebin.com/jhB4mJXE


#2

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


#3

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


#4