10. Censor


#1




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


I don't see a problem


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


#2

lets replaced the spaces with underscores so we can see the spaces, what you have:

***_***_***_

what you should have:

***_***_***

you can slice off the last space, that would be the easiest solution, you have done string slicing before


#3

thanks
result = result[:len(text)]


#4

that will work, or you can use negative numbers to slice of at the right hand sight:

result[:-1]

you can even return the slice:

return result[:-1]

so many options


#7

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