10, Censor/ Index problem


def censor(text, word):
new = ''
for letter in text:
if letter == word[0]:
control = 1
word_index = text.index(letter) + 1
check = ''
for i in range(len(word) + 1):
if text[word_index] == word[control]:
check += word[control]
control += 1
word_index += 1
new += letter
if check == word:
word_index = text.index(letter)
for e in range(len(word) + 1):
text[word_index] = '*'
word_index += 1
new += letter
new += letter
return new

It gives me the following error message:

Traceback (most recent call last):
File "python", line 29, in
File "python", line 9, in censor
IndexError: string index out of range

I don't know why it isn't working. You guys might think that i went completely nuts with that code and could be much simpler, but it is the only way i figured out to solve the problem. I would appreciate if someone can tells me what's wrong, thanks :slightly_smiling:


That error message is telling you that you are using an index to a string that would be outside the string.

for example:


There is no 54th character in that string, the last valid index there is 2.

So what you need to do is to consider if you're using the right string and if you're using the right index at that location (line 29)

You can use print statements to print out what values they hold for testing purposes.


try to use a more simple approach, with this code you can get the expected result:

def censor(text,word):
text=text.replace(word,"*" * len(word))


That's not simpler, that's avoiding the exercise altogether.

There's nothing wrong with working out the bugs in what they have there, that's great practice.

It also won't produce the expected result, since it isn't valid code.


also, i get the impression that the text.replace is deprecated, no ? I was looking at this when i tried to solve this problem, and decided not to use it. can you or someone suggest a real user friendly guide to python syntax ? I am using https://docs.python.org/2/index.html , version 2.7. adequate but not beautiful. what version of python is this little lesson intended for? i forget.


Another way to solve this problem:


Python 2.7 could be described as deprecated, it's supported until 2020

str.replace is not deprecated, no

There isn't a whole lot to learn about syntax, google if you need to look something up. Use python.org to look up functions and methods, get there by googling though.


According to instructions and train of my thought,below code to solve this problem.You do not modify data as looping a same list,so the data you need with a new list.

def censor(text,word):
new = []
new1= []
result = text.split()
count = 0
for i in result:
for i in new1:
if i == word:
number = new1.index(i)
new1[number] = len(word)''
count += 1

censored = " ".join(new)
return censored

print censor("hey hey hey", "hey")


Sorry, but could you please explain to me what split() does?


I also used the .index function to get the index and my code was exactly the same as your code besides the variable names. Found it via other sources which is good because it forces you to learn new functions. Very useful. Thanks for mentioning this.


Split takes a string of words and returns the words as a list.


If you use index and there is an identical word in the split() string, it will return the earliest instance of that word. Use enumerate in the format for INDEX,THING in enumerate(LIST OF THINGS).


solutions looks more elegance with RegEx
import re
def censor(text,word):
return re.sub(r"\b%s\b" % word, "*"*len(word),text)