10. censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#

The error mesage is:

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


The error message does not make any sense at all. What have I done wrong. Please help me.


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


#2

An additional space is placed at the very end is the problem:

"***_***_***_"

i replaced the spaces with a underscore. Now, i hope you see the problem, you need help fixing it, or do you want to try it yourself first, now you know what the problem is?


#3

I think I got it, I'll come back if I have any problems
EDIT:
Actually nevermind I need help


#4

Okay, i would simple check if the last character in your string is a space just before you return it:

if empty_str[-1] == " ":

if it is the case, slice it off:

empty_str = empty_str[:-1]

list slicing is already covered right?


#5

Yes, I've covered list slicing and thanks I got past it now.