Censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


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")


#2

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


#4

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