Censor Dispenser (3rd task)

Hi I couldn’t understand why my for loop doesn’t work it might be about string being immutable or so I don’t know really

# These are the emails you will be censoring. The open() function is opening the text file that the emails are contained in and the .read() method is allowing us to save their contexts to the following variables:
email_one = open("email_one.txt", "r").read()
email_two = open("email_two.txt", "r").read()
email_three = open("email_three.txt", "r").read()
email_four = open("email_four.txt", "r").read()

def censor(email, censor_material):
  if censor_material in email:
    email_censored = email.replace("learning algorithms", "")
    return email_censored
    return ("Couldn't find the censor_material")

#print(censor(email_one, "learning algorithms"))

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

def censor_from_a_list(email, censor_list):
  for censor in censor_list:
    if censor in email:
      email_censored_from_a_list = email.replace(censor, "")
      #return (censor, "couldn't be found in the email !")
    #return email_censored_from_a_list
  return email_censored_from_a_list

print(censor_from_a_list(email_two, proprietary_terms))

What happens every time you call the replace() method? What does it actually return? What are you then doing with the returned object?

no error, just nothing happens if I am not mistaken, I tried to put a (count = 0) code to check if I have an indention problem.Unfortunately I don’t have that :frowning:

It seems I get exactly the same email_two as before

I’am suspecting that maybe its about using = as the operant because it might delete the previous edit then print thus maybe its just printing the last edit :frowning:

Oh thank you btw, just asking important questions to myself has solved my problem :smiley:

1 Like
ef censor_from_a_list(email, censor_list):
  for censor in censor_list:
    if censor in email:
      email = email.replace(censor, "")

Creating another variable creates the problem, so I just changed that (because you know if you say a = b for the first iteration then if you say a = c for the second iteration and then if you say a = d for the third iteration your code will return a = d thus if you want to change anything with loops (by iterating) you should choose the syntax as this is (a = a(whatever you want to change)

I hope I did explain myself clearly :slight_smile:

1 Like

Aye, seems like you got it. The .replace method creates an entirely new string each time and since this was assigned to a variable that was basically overwirtten every iteration meant that almost all your chagnes were ignored.