Can someone explain why this code works in the Censor Dispenser project? (Step 3)

https://www.codecademy.com/practice/projects/censor-dispenser

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
def censor_mult_words(word):
  return email_two.replace(word, "***")
for words in proprietary_terms:
  email_two = censor_mult_words(words)
print(email_two)

Why does this censor all the words in proprietary_terms when I write:

email_two = censor_mult_words(words)`

and not when I write:

censored_email = censor_mult_words(words)

I am a beginner so please help!

1 Like

Hi thanks for sharing!

It’s a little unclear what you’re asking, so let me know if I understood wrong:

#both of these work the same
for words in proprietary_terms:
    email_two = censor_mult_words(words)

for words in proprietary_terms:
    censored_email = censor_mult_words(words)

are you getting a message error when you are running these? What’s the message error?

If you’re not getting a message error, what happens when you try to print censored_email

Hi, so when I use “censored_email” instead of “email_two” instead of printing out all the words censored it only prints out the last word censored. So I’m confused as to why this doesn’t happen when I use “email_two”.

This is what prints when I use “email_two”:
Lots of updates this week. The ***s have been working better than we could have ever expected. Our initial internal data dumps have been completed and we have proceeded with the plan to connect the system to the internet and wow! The results are mind blowing.

She is learning faster than ever. Her learning rate now that *** has access to the world wide web has increased exponentially, far faster than we had though the ***s were capable of.

Not only that, but we have configured *** *** to allow for communication between the system and our team of researc***s. That’s how we know *** considers ***self to be a ***! We asked!

How cool is that? We didn’t expect a personality to develop this early on in the process but it seems like a rudimentary *** is starting to form. This is a major step in the process, as having a *** and *** will allow *** to see the problems the world is facing and make hard but necessary decisions for the betterment of the planet.

We are a-buzz down in the lab with excitement over these developments and we hope that the investors share our enthusiasm.

Till next month,
Francine, Head Scientist

And what prints when I use “censored_email”:
Lots of updates this week. The learning algorithms have been working better than we could have ever expected. Our initial internal data dumps have been completed and we have proceeded with the plan to connect the system to the internet and wow! The results are mind blowing.

She is learning faster than ever. Her learning rate now that she has access to the world wide web has increased exponentially, far faster than we had though the learning algorithms were capable of.

Not only that, but we have configured her personality matrix to allow for communication between the system and our team of researchers. That’s how we know she considers *** to be a she! We asked!

How cool is that? We didn’t expect a personality to develop this early on in the process but it seems like a rudimentary sense of self is starting to form. This is a major step in the process, as having a sense of self and self-preservation will allow her to see the problems the world is facing and make hard but necessary decisions for the betterment of the planet.

We are a-buzz down in the lab with excitement over these developments and we hope that the investors share our enthusiasm.

Till next month,
Francine, Head Scientist

can you share your code again, but in a continuous fashion?

It might be an indentation thing.

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

def censor_mult_words(word):
  return email_two.replace(word, "***")
for words in proprietary_terms:
  censored_email = censor_mult_words(words)
print(censored_email)
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"] 

def censor_mult_words(word):
  return email_two.replace(word, "***")
for words in proprietary_terms:
  email_two = censor_mult_words(words)
print(email_two)

Like this? Sorry if I’m not understanding I’m still pretty young haha.

This makes more sense! Check the return value of your function censor_mult_words.
Let me know if you catch it now… :smiley:

Hmmm… is it because I am returning email_two and not censored_email? Is that why it only censors the last word on the last iteration when I use censored_email? And everytime my for loop runs it’s saving the censored version to email_two so when I print it out it prints out with all the words censored?

1 Like

Bingo. You got it. If you play around with the parameters you may be able to make your function work for any email.

2 Likes

Wow! Thanks so much!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.