Censor Dispenser (proprietary_terms)

`Hello everyone
this is my first post here and i would appreciate your help with the following:

I have written this code but it only change the first string from the list:

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

def censor_list(email):
  for i in proprietary_terms:
    if i in email:
      censor = email.replace(i,'###') 
    return censor

print(censor_list(email_two))

what am i missing here?

When you post code, always preserve the required Python indentations by making use of the </> icon that is in the menu bar atop the text box when you type.

I am quite certain the problem has something to do with the indentation level of your return statement.

2 Likes

Hello patrickd314
Thanks for you notice. Honestly I have a huge issue with the indentation.
But I don’t think this is the problem because I have tried return with all the indentation but it didn’t work

… also, note that, each time around, the variable censor is assigned the value, email.replace(i,'###') , which is email with one replacement

An analogy:

Challenge: replace each vowel with the character “x”:

my_str = "now is the time for all good men"

for vowel in 'aeiou':
    new_str = my_str.replace(vowel, 'x')
print(new_str)

Run it. Now run it again, this time with the print() statement indented to be under **new_str = **.

What happens? How to fix it?

3 Likes

i did not quite get your point here with the analogy.
i did run your code:
1/ when print() is under new_str, it prints the string multiple times

Do you notice the position of ‘x’ each time?

What is desired is “nxw xs thx txmx fxr all gxxd mxn”
What is actually happening?

1 Like

each line of the output changed in sequence of ‘aeiou’
the first line only the ‘a’ changed to ‘x’
the second line only the ‘e’ changed to ‘x’
the third line only the ‘i’ changed to ‘x’
and so on…

but we did not get the desired string

what i just do not understand is why the first string in the list changed and not every string even though i looped through the list

If you start out with "abc"
and you replace 'a' with 'x' then you would obtain "xbc"
If you start out with "xbc"
and you replace 'b' with 'x' then you would obtain "xxc"

You can try this yourself.

If that is what you think your code is doing, then, the fact that the result isn’t x’s for all the replaced letters should tell you that, no, this is not what was happening.

After that you can start looking closer at what was being done. You have to look, you have to compare.

Keep in mind that this is a program that you wrote yourself, nothing is hidden from you, nothing is preventing you from looking at any of it. But you do have to write code that extracts the information you want to look at.

Other people may be able to tell you what the difference is, but how do they figure that out? They did exactly that, they looked closer, and that’s something you can do too.

3 Likes