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 print(censor('hey guys hey', 'hey'))
Hi! I was wondering if anyone could explain this part of the code to me:
for i in words: if i == word: words[count] = stars count += 1 result =' '.join(words)
For this part, if words[count] = stars and then count += 1, if for example the text is “hey guys hey” and the word is “hey,”
Wouldn’t words be replaced by stars, followed by words and words ? Since we are only increasing the count by one. However, the code is working properly but I don’t really understand how the “count” part works.
Thanks in advance!!