Hi, I've created my code but it doesn't censor the words as intended and I can't work out why - any ideas?

def censor(text,word):
    text = text.split()
    for i in text:
        if i == word:
            i.replace(i,"*" * 4)
    return " ".join(text)

print censor ("this hack is wack hack", "hack")


i gets assign the values of the list one by one, but this won't persist. change you make to i will be discarded in the next run of the loop since the next value in the list has to be assigned to it.

you could use range() which if done correctly, could provide you with the indexes to manipulate the text list.

also, replace() will simply replace all instances it can find. This allows for a one line solution. But then it is a bit of boring to use use a built in function like that to solve the problem

If you need more help, post an updated version of your code + question


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