Censor not working when it should be working



The answer prints correctly but I keep getting the error message saying "Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***"."

Below is my code. Please help me(not sure why it is shown unindented):

def censor(text, word):
new = ""
split = text.split(" ")
for each in split:
if each == word:
new += "*" * len(word) + " "
new += each + " "
return ne


Did you mean to say
return new


There is a blank space at the end of your output: "*** *** *** ". Hard to notice at first, but it's different from what they want. Find a way to remove that space and it should accept your method.


@chipjumper36731 Thanks! I should change it to "new"
@melindabray Thank you so much!


Here is how I did it.

def censor(text, word):
    word_old = word
    for letter in word:
        word = word.replace(letter, '*')
    while word_old in text:
        if word_old in text:
            text = text.replace(word_old, word)
    return text


You're welcome @ryanh1002. Happy to help! :slight_smile:


You need to remove the last blank space from your solution. Try return new.rstrip()

This should remove the last white space at the end and allow you to complete the assignment with your code.

Good luck!


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