10. censor - My code is right but it says its not?


#1

I have this code and when I run it the error I get is

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

Seems like everything works for me so ?

def censor(text, word):
    star = "*" * len(word)
    broken = text.split()
    final = ""
    for i in broken:
        if word in text: 
            final += star + " "
        else:
            final += i
    return final

#2

This is adding white space to the end of the return string.

        if word in text: 
            final += star + " "
        else:
            final += i

Instead of building a string, build a new list, then join() the final return.

final = []
# ...
    if i == word:
        final.append(star)
    else:
        final append(i)
# ...
return " ".join(final)

#3

Okay thanks! Seems more efficient to do it that way.