Censor not working when it should be working


#1

Hi,

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) + " "
else:
new += each + " "
return ne


#2

Did you mean to say
return new
?


#4

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.


#5

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


#6

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

#7

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


#8

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!


#9

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