Need some help on Censor Dispenser

Hey guys, wondering if you can help with this as I am stuck on step 3. First I have a question about step two though.

For step two: the .strip() function doesn’t work on the email file, but the .replace() function does. However if I save email_one as a string rather than a .txt file, then the .strip() function will work. Can anyone explain why that is?

So for example, this works:

  email_split = email.replace("learning algorithms", "*" * len("learning algorithms"))
  return email_split

but this doesn’t

  email_split = email.strip("learning algorithms")
  return email_split

I don’t get why…

Now on to my main question. For step three, my “for” loop is only returning the last value, rather than iterating through the list.

def proprietary_terms(email):
  proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithms", "her", "herself"]
  for i in proprietary_terms:
    if i in email:
      email_propterm = email.replace(i, "*" * len(i))
  return email_propterm

#print(email_two)
print(proprietary_terms(email_two))
    

So when I call the function the only word that gets replaced with “*” * len(i) is “herself”. I cant figure out why thats happening. When I indent it (as expected) it only iterates through the first value of i, usually when that happens I unindent and it iterates through the whole list, but not this time.

Any help would be greatly appreciated. Thanks.

you could read str.strip’s documentation. to say it doesn’t work … you’d have to know what it’s supposed to do first

not sure how you’d ask strip to operate on a file, my guess is that’s not what you did

if you made it all the way to the last value, then doesn’t that suggest that iteration did in fact happen?
it might not have been the result you expected, but to figure out what’s up with that you’d observe each individual action and compare to what you meant should have happen. what values did you feed into what operations and what came out as the results?

suggestion: print out what you’ve got after each action that you carry out.

"you could read str.strip’s documentation. to say it doesn’t work … you’d have to know what it’s supposed to do first

not sure how you’d ask strip to operate on a file, my guess is that’s not what you did"

Yeah, .strip() is supposed to remove whatever character is specified in the .strip() command from a string. My point being why are we using text files to practice what we learned about strings if the commands don’t work on the text files?

“if you made it all the way to the last value, then doesn’t that suggest that iteration did in fact happen?
it might not have been the result you expected, but to figure out what’s up with that you’d observe each individual action and compare to what you meant should have happen. what values did you feed into what operations and what came out as the results?”

That’s the thing, it seems like its iterating through the loop, but the function is only acting on the last index of the list. I defined a list, then created a for loop for each item in the list, then if any of the values in the list match a value in the email, replace it with “*” * len(i). So what exactly am I missing? It looks right, but obviously isn’t working…

what makes you say there’s a text file anywhere
have you for example looked at the type of your value?

That’s over-generalized. go read what it does.

if you call a function it’s going to do its thing. they don’t take holidays. look at the input and output to that specific operation, did it behave as promised?

How do you observe something? Print it. No?
Put some prints in there.
You may want to use a shorter text for the email to keep the output manageable.

def proprietary_terms(email):
  proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithms", "her", "herself"]
  for i in proprietary_terms:
    if i in email:
      input = email, i, "*" * len(i)
      print('input:', input)
      email_propterm = email.replace(i, "*" * len(i))
      print('output:', email_propterm)
  return email_propterm

You may also want to test the operations you are using in isolation from the rest of your code.
You could for example make a function for the current problem that you are solving, and then when you’ve finally solved it you can call that function from the rest of the code.
Then you would have a function that you could test separately. If you run one big blob and look at the final result then you’re not going to find out a whole lot about the individual steps you carried out.

An example of input you could use is “a b c d e f”, remove “cdf”.
That’s representative of the problem, but small enough that you can print out everything being done and still be able to read it all to verify that the right things are being done. When some print shows the wrong thing having been done, then you’ve also identified which operation it is.


and, by the way, if something isn’t there then replacing it will have no effect. this condition is redundant.

Sorry I don’t know how to quote text, this forum’s ui isn’t very good.

“what makes you say there’s a text file anywhere
have you for example looked at the type of your value?”

The text files attached. I’m not sure if they are converted to strings or not with the open and read commands (I don’t know those), but if they are shouldn’t the strip function work? Since it didn’t I assumed they weren’t strings

“That’s over-generalized. go read what it does.”

I am just going off what was taught in the lesson, which I would think there wouldn’t be anything not taught in the lesson in these practice problems. Where do I read about it?

“if you call a function it’s going to do its thing. they don’t take holidays. look at the input and output to that specific operation, did it behave as promised?”

I put your input/output code into the .py and it returned the email multiple times, each time replacing one of the words from the list with “*” * len(i). So it appears to be working, but the value returned is not working…

a file is an operating system feature. your code only sees text.

what does work mean for strip? gotta start there before you say it’s not working.

something to get in the habit of:
google.com/search?q=python+str+strip
there’s also some degree of built-in documentation, you can view docstrings of various objects using the help function

things work only because you align things exactly the way you need them to be aligned. so you’re missing some attention to detail there. you’d look at what some operation does, you’d look at what you have to use as input, you’d look at what you would then do with the output. you have to match things up every step of the way. there’s nothing else that does that.

do you for example see a single use of replace misbehaving?
or some other operation? which particular operation isn’t doing what you want?

something important to realize:
it’s not wrong because you’ve made some silly mistake somewhere that you can’t see
you get it right by observing each required step and fixing it where it isn’t doing what you want.
and observing doesn’t really take any special knowledge. you look before, you look after, you compare to what you intended. that’s all there is to it.

You don’t end up with a wrong result by doing all the right things. If you can observe each thing that you needed to happen, then you DO have the correct result. That’s completely synonymous! Where does it go wrong? Are those the things that would happen if you did it manually? Using smaller input would make it easier to inspect.

I followed your link and the first two results didn’t offer any further documentation beyond the lesson, so what is it that you want me to know about it?

I tried doing this, by creating a variable test = “abcdefg” but just keep getting a name error that email_propterm isn’t defined, even if I define it beforehand…

So what should I be doing instead?

I know, but I don’t know where its doing something different than what I’m trying to make it do, thats why I am asking for help…

I don’t know, that’s why I am asking for help. Because I can’t figure out on my own where its not doing what I want it to do, or how to make it do what I want it to do. As stated above, I tried using a variable test = “abcdefg” but I keep getting a name error, even when I define the email_proterm variable beforehand.

What about my code should I change?

what’s the result of this?

'faf'.strip('a')

you said it removes a. does a get removed?
you said it does this for string but not file. that’s a string.

strip(self, chars=None, /)
    Return a copy of the string with leading and trailing whitespace removed.
    
    If chars is given and not None, remove characters in chars instead.

leading and trailing

if there’s nothing to do, do nothing

the problem itself isn’t important at all to solve
the real thing going on here is you learning to control what you’re writing
you have to go in with the expectation of being able to observe everything that you write. if you write something that’s out of your control then you’re completely lost and nothing else will make it right. you can’t move on before you control what you’re currently working on

email = "a b c d e f"
terms = ['a', 'd', 'e']

def proprietary_terms(email):
  for i in terms:
      email_propterm = email.replace(i, "*" * len(i))
  return email_propterm

same code. redundant if-statement removed. email and proprietary terms replaced, your function and list had the same variable name, those should probably be different.

print out the replacements after each step.

No, Thank you, I misunderstood what .strip() does. So it only removed things on the beginning and end of the string, not in the middle, correct?

But there is something to do, but I am clearly not doing the correct thing.

I wholeheartedly disagree, solving the problem helps me understand what I’m doing. When id doesn’t do what I want it to do, knowing what its doing that I don’t want it to do helps me understand what NOT to do. If I can’t solve it I don’t learn anything, If I don’t know what I did wrong, then I don’t learn anything. right know I don’t know what I’m doing wrong or how to solve it.

Thanks, I tested this but I got the same result… it printed a b c d * f

That’s the final outcome. What happened after each step?
And, what would the right output be after each step?
And, what must then necessarily be wrong? That’s a matter of subtraction. this - that = difference

this is every-day logic, not code related.

a b c d e f
* b c d e f
* b c * e f
* b c * * f
a b c d e f
* b c d e f
a b c * e f
a b c d * f

What is the first one doing that the second one is not?
If you replace a, then, that information is GONE. You cannot get it back. And yet, there it is again. Where was that a found? Where should it (not) have come from?

The first one is making sure the “a” gets replaced with a “*”. But I’m not sure how to change my code to make it do that. I know that’s the problem, but I don’t know how to solve it.

No, you have input showing that you did that.
You already did that.
What should happen next, and what happens next instead?

From the original email text?

the original

Should you be looking at that?

So. No mysterious python-behaving-oddly.

Just straight up writing the wrong thing. Observable. Adjustable.

You observe, narrow down, compare.
You’ll be bad at it for a while but as you do it over and over you’ll do it faster and recognize patters and so on.

So I don’t want it to keep referencing the original email file every time it iterates through the list? I need it to remove the first one, store it in a new variable, then reference that one when it iterates through the next item in the list? Or am I way off here?

Well. Do you?
That’s got nothing to do with code, that’s got to do with … hey what should at all happen.