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


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

#2

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.


#4

how do I treat the variable as an index?


#5

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)

#6

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