Censor


#1




could you please help me why my code is not working?!

def censor(text,word):
sp_text = text.split()

for v in sp_text:
    if v == word:
       v = "*" * len(word)
    sp_text.insert(sp_text.index(word),v)
    sp_text.remove(word)
return " ".join(sp_text)
print sp_text

#2

inserting and removing causes massive problems. If you don't believe me, run it through a visualizer (link), add some print statement to see what is happening

There are two common approaches to solve this problem, either use range() in your loop which will give you indexes so you can replace words which need censoring in sp_text

or make an empty list, use if/else, if the word needs censoring, append asterisks to new list, else just append the current word in sentence to the new list


#3

thank you for clear explanation!


#4