Part Three of Censor Dispenser Challenge, Help Needed

Hi there,

I am working through the Censor Dispenser Challenge for the strings section of learning Python, and I’m stuck on part three. You can find the link here:

After trying for a while with no success, and after searching the forum for a while but finding nothing that answers my issue, I am starting up a thread here.

The third question asks the following: “Write a function that can censor not just a specific word or phrase from a body of text, but a whole list of words and phrases, and then return the text.” We are given the text (i.e. an email), and the list of words to censor is also given (shown below):

proprietary_terms = [“she”, “personality matrix”, “sense of self”, “self-preservation”, “learning algorithm”, “her”, “herself”]

Here is my code:

def censor_list(email, lst):
for i in range(len(lst)):
censored_email = email.replace(lst[i], ‘X’)
return censored_email

print(censor_list(email_two, proprietary_terms))

For some reason this returns the email_two input in my console, but with only the last word in the list ‘herself’ censored with the X.

I would appreciate any input on how to fix my code to censor all the list elements, or, honestly, some clean code that just solves the problem so I can learn from that.

Thanks very much!

The ‘for’ and ‘return’ pieces of the function are correctly indented, but the above post doesn’t reflect that

Sounds like you haven’t tried to observe what’s happening.
You could for example print out what’s being done step by step… the first step to “fixing” the problem is to understand what the problem is, don’t try to skip that part.

You’re never going to start writing code that’s right from the start. You get there by observing and adjusting.

1 Like