10, Censor/ Index problem


#1

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
else:
new += letter
break
if check == word:
word_index = text.index(letter)
for e in range(len(word) + 1):
text[word_index] = '*'
word_index += 1
new += letter
else:
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:


#2

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

for example:

'bob'[53]

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.


#3

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))
return(text)


#4

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.


#5

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.


#6

Another way to solve this problem:


#7

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.


#8

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:
new1.append(i)
for i in new1:
if i == word:
number = new1.index(i)
new1[number] = len(word)''
new.append(new1[number])
count += 1
else:
new.append(i)

censored = " ".join(new)
return censored

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


#9

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


#10

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.


#11

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


#12

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).


#13

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