What is wrong whit this code? Censor


def censor (text, word):
text = text.split()
for i in text:
if text[i] == word:
text[i] = len (word) * “*”
return “”.join(text)

<do not remove the three backticks above>


It’s your for loop. You loop through i in text, which is a string, and then try to use i as an index in text[i]. Instead you should loop through a list of numbers ranging from 0 to the length of text. If you replace for i in text: with for i in range(0,len(text)): your code should run.

However, the function will return text that is joined together without any spaces, since .split() removes them. If you replace return “”.join(text) with return " ".join(text) it works fine.


I was stuck and tried the suggestions and came up broke. Doesn’t replace across all iterations of the word

censor(text, word):
    text = text.split()
    for i in range(0,len(text)):
        if text[i] == word:
            text[i] = len(word) * '*'
        return " ".join(text)        

what am I missing here? the IDE replies with "Your function fails on censor(“hey hey hey”,“hey”). It returns “*** hey hey” when it should return “*** *** ***”.


Too much indentation. return should be in line with for.