10. Censor - Why won't it replace the word?


In my code below, it splits and joins fine, and when I add print statements it looks like it's iterating over the list fine, but for some reason my if: statement isn't replacing the word. I'm sure it's something simple, but I can't find the problem.

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

def censor(text, word):
    phrase = text.split()
    cen_word = '*' * len(word)
    print phrase
    for i in phrase:
        if i == word:
            i = cen_word
    return " ".join(phrase)

print censor('forget this blank blank nonsense its a pile of blank','blank')


because i gets re-assigned in every run of the loop:

for i in phrase:
   print i

so i isn't stored anywhere, it won't persist. You could create an empty list before you loop, and then append censored/uncensored words to the empty list with an if/else clause? And then in the end, join the list into a string


Thanks, that did the trick. Don't know why I thought it would persist.