Censor Dispenser

Hello everbody!
I would appreciate your help with censor dispenser task, part 3, where you have to censor all words from a list and replace them with allowed terms.
This is the task:
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.
Mr. Cloudy has asked that you censor all words and phrases from the following list in email_two .
Here is my code:

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
ok_terms = ["it", "individual pattern", "understanding", "safety", "special feature", "its", "itself"]

def censor_two(email):
  for term in proprietary_terms:
   if term in email:
     email_censored = email.replace(proprietary_terms[proprietary_terms.index(term)], ok_terms[ok_terms.index(term)])
     return email_censored
   
print(censor_two(email_two))

Here is the error message I get:

Traceback (most recent call last):
  File "script.py", line 22, in <module>
    print(censor_two(email_two))
  File "script.py", line 17, in censor_two
    email_censored = email.replace(proprietary_terms[proprietary_terms.index(term)], ok_terms[ok_terms.index(term)])
ValueError: 'she' is not in list

Here is a link to the task: https://www.codecademy.com/practice/projects/censor-dispenser
Thank you very much in advance!

here:

ok_terms[ok_terms.index(term)])

term has a value of "she", this string is not in ok_terms list. Furthermore, it seems you need censor the word, so "she" has to become "XXX"

Thank you for your answer, stetim94! It makes sense “she” is not in ok_terms, so that was a problem. But could you please explain, what do you mean by “tensor the word”? I’ve corrected that part of the code, which gives the index of the element in ok_terms (it is the same index as of the element from proprietary_terms, so I put the index as [proprietary_terms.index(term)]). Now I don’t get the error message, but the code doesn’t replace words.

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
ok_terms = ["it", "individual pattern", "understanding", "safety", "special feature", "its", "itself"]

def censor_two(email):
  for term in proprietary_terms:
   if term in email:
     email_censored = email.replace(proprietary_terms[proprietary_terms.index(term)], ok_terms[proprietary_terms.index(term)])
     return email_censored

Un-indent return so that the for loop can complete.

Also, check out the function enumerate(). It would help to simplify your code considerably.

(EDIT) Just noticed: you are continually over-writing email_censored with the latest replacement in email. The idea is to have the replacements be cumulative. This will be complicated by the fact that each replacement changes the indexing.

1 Like

ok_terms are not relevant for this task, you just need to censor (i had a typo in the previous post) the words by replacing them with 'X'.

Hey Patrick,

i am also challenging the censor dispenser at that moment. My code is nearly the same.

So when i am running only the first term in “proprietary_terms” gets censored.
But shouldn’t it work because i run the for loop " term in proprietary_terms:" ?

And if the email_censored is over-written shouldn’t be the last index replaced (“herself”) printed?
In my case “she” is replaced

Best regards,
André

Yes, that is a good observation, that you would expect only the last item to show up. However, if return is indented as we see above, the function will complete after only one trip through the for loop (remember that return halts processing of the function).

Good point! I outdented the return and wich surprise the last string in the list is censored.
Now this makes sense.

But the over-writing of email_censored still is the main issue in solving this.
Nevertheless i trying to solve. You think this is the most elegant way to solve this problem or is this approach not professional? :wink:

So for now it looks quite good:

def censor_email_two(email):
for term in proprietary_terms:
email = email.replace(term, “#” * len(term))
return email
print(censor_email_two(email_two))

but for example in the word researchers its getting censored like researc###s. So i have to tell him he only searches for the words not surrounded by other letters. I will try with something like witespaces before term. Lets see this evening :slight_smile:

1 Like