Censor refinement


Good afternoon! I was wondering if anyone might provide some insight into the issue I patched up near the end of my for loop. For some reason I kept getting an error saying: Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns " *** *** *** " when it should return "*** *** ***".

I interpret this to mean something in my if statement was placing a space before the first word in my string, but I can't figure out why I kept having this issue and no one else seemed to. As you can see, I fixed it using the .lstrip() command, but I wanted to know if anyone had any suggestions for a more elegant solution.

def censor(text, word):
    text = text.split()
    print text
    count = len(word)
    new_text = ''
    for index in text:
        if index == word:
            new_text = new_text + " " + ("*" * count) 
            new_text = new_text + " " + index 
    return new_text.lstrip(" ")

print censor("hey hey hey", "hey")


your code works fine

try refreshing page/different browser, it seems your browser is sending cached data


When you add a new word to new_text, you do it after a " ", and that makes the console see it as a mistake. It should be "*** *** ***" without the space at the beggining.


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