10/15 Censor - What am I doing wrong?


Here's what I have:

def censor(text, word):
text_split = text.split()
join_text = []
for i in range(len(text)):
    if text_split[i] == word:
return " ".join(join_text)

It seems I'm getting an index error. What am I doing wrong? Thanks!


This route seemed easier...wasn't comfortable with the splitting.

This works just as well using the replace operator.

def censor(text, word):
censored_word = ""
for c in word:
    censored_word += "*"
if word in text:
   new_text = text.replace(word, censored_word)
return new_text


@mmartin712 this is why you get that error. check out this excerpt below

#imagine I have text
text = "they know they know they know"

#call len on text
print len(text)   #29

#now i split text. default delimeter is white spaces
text_split = text.split() #['they', 'know', 'they', 'know', 'they', 'know']

#call len on text_split
print len(text_split) #6

Now the problem in your code is the for loop you are using range(len(text)) which is probably bigger than the range(size) of text_split(which is a list) so when you write

if text_split[i] == word:

you will get an error when you the index i is bigger than the size of text_split. Now with all that said all you have to change is one thing in your code.
Change the for loop block to

for i in range(len(text_split)):


I found this work, try it

def censor(text, word):
if word in text:
new_text = text.replace(word, "*" * len(word))
return new_text


That was it. Thank you!


**good job

def censor(text,word):

return c

print censor('anand is a good boy','good')


I guess the point of all these exercises isn't cheating and trying to use built-in functions, but doing them the long way to practice fundamentals! So you had to write something like this instead:

def censor(text, word):
    words = text.split()
    equivalent = "*" * len(word)
    for index, item in enumerate(words):
        if item == word:
            words[index] = equivalent
    return " ".join(words)


Personally I think you should use range(0,len(text)) on line four. Maybe that will help.


The program is working correctly. And your proposal doesn't really change much:

for index in range(0, len(word)):
    if words[index] == word:
        words[index] = equivalent


this is how i did it:

def censor(text,word):
for x in chop:
if x==word:
return ' '.join(answer)


I like how clean this is, but how would you go about editing it so that the censored word will be filtered out regardless of whether the letters are uppercase or lowercase? i'm just not sure where to put ".lower" is what I'm getting at.

edit: ahhh, sorry this appeared so low on the page, it was in reference to javacoder41347's post. i'm not sure how to properly comment yet, this is my first one


Here I have some 'clean' code for you!

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

This as about as concise as you can get this code.

Good luck and have fun!

import time
start_time = time.time()
def censor(text,word):
    a = text.split( )
    for i in range(len(a)):
        if a[i] == word:
            b.append("*" * len(word))
    return " ".join(b)
print("--- %s seconds ---" % (time.time() - start_time))


Nice try! And I found out that with out the "if" part the method also works.

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


without using replace (only using functions that hint thing give me):

def censor(text, word):
    censored = []
    for w in text.split():
        if w == word:
            censored.append('*' * len(word))

    return " ".join(censored)


string.replace() is great but it will replace every instance of the keyword, whether it is a separate word or not.
For eg: text ="this cat is called catherine"
word = "cat"


I tried and find this works:

def censor(text, word):
text = text.lower() # text in lowercase
word = word.lower() # word in lowercase
if word in text:
new_text = text.replace(word, "*" * len(word))
return new_text


Hey! This is what I came up with, and then I thought I would look around for other soltions. I've posted a question, because this solution displays properly, but doesn't actually pass :frowning:


def censor(text, word):
if word in text:
return text.replace(word, "*" * len(word))


@munim_uiu refer to @zeziba's code in this same thread on post #12 to see how you can improve your code