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’)
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!