Need Help with Censor Dispenser

Hello everybody, i would like some help with the project I mention above.

To be more specific, I am currently on the last task, the one that asks me to censor everything from the given email_four.

This is my code:

censor_all = proprietary_terms + negative_words

def censor_four(input_text, censored_list):
    splitted_text = input_text.split(" ")
    for word in range(len(censored_list)):

        if word in splitted_text:

            this_word = splitted_text[word]
            censored_this_word = ""
            for i in range(len(this_word)):
                censored_this_word=censored_this_word + "X"
            input_text[this_word] =  input_text.replace(this_word,censored_this_word)

            word_before =  splitted_text[word-1]
            censored_word_before = ""
            for i in range(len(word_before)):
                censored_word_before = censored_word_before + "X"
            input_text[word_before] = input_text.replace(word_before,censored_word_before)


            word_after = splitted_text[word+1]
            censored_word_after = ""

            for i in range(len(word_after)):
                censorer_word_after = censored_word_after + "X"
            input_text[word_after] = input_text.replace(word_after, censored_word_after)

    return "".join(input_text)


print(censor_four(email_four,censor_all))

Unfortunately all it does is to return the exact email_four with no changes at all. Any ideas why??

Thanks in advance!

There are events there that you want to be happening, right? Are they happening?

What can you do to observe what is being done?

Something you might start checking is whether you are correctly isolating the words 3 at a time, and when you have that working you can work on what you would do with those 3 words.

I suggest avoiding str.replace and str.index since you aren’t controlling which particular instance is being replaced/found. Better if you make the change at the current location.

Since you will be both reading and writing, it is probably much easier to have two separate values for them, one that you read from and another that you write to. That way you avoid any complicated interference.

There are multiple problems. The first is that word is assigned an integer value because of this:

for word in range(len(censored_list)):

And on the following line you attempt to see if word (an integer) is contained within a list of strings:

if word in splitted_text:

Since that can never be true, your loop will never perform any action.

I don’t know what you want to do there, but either you want word to be one of the censored words in censored_list, rather than an index of a censored word, or you want to see if censored_list[word] is in splitted_text. Either way, once you correct this issue, there are further issues that you’ll need to address, depending on which way you intend to go here.

Once you make the adjustment, as a “quick and dirty” means of debugging, you can place print calls throughout your code to see what’s going on.