10.Censoring....why doesn't my code work(it returns the same text with out censoring)

def censor(text, word):
  text = text.split()
  for w in text:
    if w == word:
      w = "*" * len(w)
  return " ".join(text)


The text is not being changed. w is a readonly value, not a list element, per se. Anything we assign to w is not assigned to the list.

If you treat the variable as an index, then it can point to the element you wish to change.


how do I treat the variable as an index?


By using a range in the loop.

for i in range(len(text)):

Now i will be a number from 0 to len(text) -1, and can be used as an index.

if text[i] == word:
    text[i] = "*" * len(word)


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