"censor" - 10/15 of "Practice Makes Perfect"


#1

Hello!

I've come up with this code for the exercise, and my function is just returning "text." I feel like I'm close. I know that the line "text_list[i] = "*" * len(i)" is wrong, but I don't know how to assign the actual index to be replaced.

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

Thanks.



So, I solved it on my own! I'll leave this thread up with my solution, in case it's any help to others :slightly_smiling:

def censor(text, word):
    text_list = text.split()
    for i in text_list:
        if i == word:
            text_list[text_list.index(i)] =  "*" * len(i)
    return " ".join(text_list)

#2

Hi @wisebud,

The solution was to, in this line:

text_list[i] =  "*" * len(i)

use the .index() of i instead of i itself.

I had to mark this post solved, so posted like this :wink: