def censor(text, word): words = text.split() result = '' stars = '*' * len(word) count = 0 for i in words: if i == word: words[count] = stars count += 1 result =' '.join(words) return result
I have trouble understanding ‘count’ here. ‘words’ refers to the text split up in words. The ‘for’ loop searches for an element in ‘words’. So far so good i suppose.
We want to find an element in ‘words’ that corresponds with whatever we assign ‘word’ to in calling the function. ‘words’ is a list? so we search for an index?
words[count] = stars
explained means as much as the text split into elements, where we take index zero, because count equals zero, to equal the length of the word we want to censor in asterisks? In other words, [count] is performing the task of finding the element we want in the list? Im obvi confused and very new to coding, any input appreciated.
I guess what i dont understand is how ‘words’ succeeds in finding the right element in the list?
Also, why can’t we simply say for every instance we find of ‘word’ in ‘text’ to equal ‘word’ with ‘stars’? Why do we need to make use of indices?
Thanks in advance