10 Code is right but still fails


#1


Oops, try again.
Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***".


def censor(text, word):
    spl = text.split()
    new = ""
    for wrd in spl:
        if wrd == word:
            new += ("*" * len(wrd) + " ")
        else:
            new += wrd + " "
    return "".join(new)


#2

Your code is adding a space after each word. Hence, even on the last word, it adds one space after it. So, when the exercise expects "*** " it gets "***" - notice the extra space after the last censored word.

You need to add one extra operation to output the correct string. Can you guess what it might be? :wink:


#3

Not so right after all then...

def censor(text, word):
    spl = text.split()
    new = ""
    for wrd in spl:
        if wrd == word:
            new += "*" * len(wrd) + " "
        else:
            new += (wrd + " ")
    return new.rstrip()

This is what I changed it to which works

Thanks!