8/10 censor


I’m trying to write a function that takes two input, text and word, then returns the text with the chosen word replaced by asterisks.
I split the text and put items into a list first, loop through each item in the list, then replaced the item which is the same as the chosen word by asterisks, finally join them together. But my code doesn’t work.

def censor(text,word):
word_list = text.split(’ ')
for item in word_list:
if item == word:
item == ‘*’ * len(item)
censored = ’ '.join(word_list)
return censored


item contains read-only values from the list, any changes made to this variable won’t persist


item refers to the same value that your list refers to, but changing what item refers to has no effect whatsoever on the list, you’re just making it point at something else

Also, you’re not doing even that, because == doesn’t do assignment


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