Censor space help


#1

Hi everyone, getting stuck on the censor exercise. Basically my answer is adding a space to the last word. Am not sure how i can make my code not add a space if it is the last word. Any ideas? I could do return result[0:len(result)-2] but am not sure why i am getting an error on that too. Any help is appreciated!!


def censor(text,word):
    result = "" 
    for x in text.split():
        if x == word:
            for x in range(len(word)):
                result += "*"
            result += " "
        else:
            result += text
        result += " "
    return result


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


#2

to only slice of the last character, don't use -2, -1 is enough, -2 will slice off the last two characters


#3

hi i get this error when i use -1
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***". Any idea why?


#4

you insert two spaces after each word, lets replace the spaces with underscores:

***__***__***__

oops, one space between every word is enough, and then you need to slice off the additional space at the end


#5

ok got it! thanks for the help!
here's my final code:

def censor(text,word):
    result = "" 
    for x in text.split():
        if x == word:
            for x in range(len(word)):
                result += "*"
        else:
            result += x
        result += " "
    return result[0:len(result)-1]

#6