Censor Dispenser

I’m kind of stumped on Censor Dispenser. I don’t even know where to start. Is there some place I can go that will give me a better understanding on how to tackle the logic of python? I feel like I understand the basic topics but trying to put it all together and construct something I don’t know what pieces to even start with. I know what I want to do but like I said how does this all fit together?

1 Like

I would start by writing out what you want to do by hand in sentences (and maybe even draw a diagram).

Then you can highlight for yourself what feels easy to do, and what feels vague… and then you can ask or look for questions about that specific thing (how do I manipulate a list, how do I do x, etc.).

2 Likes

Hey, I can relate to how you feel. You have a task but have absolutely no idea where to start. This is my advice:

Understand the task

In whatever you do, have a clear understanding of the objective. Otherwise, you will jump headfirst into a task with a skewed understanding of what you have to do, and get frustrated when it doesn’t work the way you want it to.

Example:

Task: Return the amount of times a word that starts with F or ends with S occurs in the list.

We can start picking out the important bits.

  • we obviously want to return a number
  • we want to find the words that start with F or end with S

Start thinking about it in simple terms

Well, if we want to find the letter a word starts with, we need to look at the first letter.
We know we can select parts of something with indexing, so:

first_letter = word[0]

Great! We also know how to use if statements that check for a condition.

if word[0]=="F":

Hm, I want to go through the entire list, how would I do that?
We could loop:

for item in lst:

I want to keep a count. When you are counting you start from zero, and go up by 1. I know the operator += can add things to a number so:

count=0
for item in lst:
  count+=1

Go step by step

Think about what you have learned and how you can use it in situations like these. Also, don’t forget we have resources like Google or the Python docs.

Putting it all together

def function(lst):
  count=0
  for item in lst:
    if item[0]== "F" or item[-1]== "S":
      count+=1
  return count

This is my thought process 90% of the time, and I find it works really well, especially when it comes to difficult tasks like these. (It took me over a week to get past the first task on Censor Dispenser!)

Hope this helps!

2 Likes

I was able to figure out number one. However, I’m not sure I understand the steps of number two. In trying to find X term in email_two, I guess I’m confused on how to approach this.
Here’s my thought:

  • Loop though proprietary_terms
  • Run some kind of search function to find the index position of each of the words, and where they are in the string
  • Delete word

I’ve been reading a ton of websites and I can find the different .find and .replace (not sure what these things are called?). I see a return of the index but I don’t understand what I am supposed to do with the information or even what it means.

How do I find multiples of the same word? How do I loop through a list when Python says I can only send though an integer?

Could you post a link to the project?

I no longer have PRO so I can’t help out, but here’s the link.

2 Likes

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 .

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

Understand the task
Mr. Cloudy wants us to:

  • censor the words in email_two that are from the list proprietary_terms.

Start thinking about it in simple terms
We want to censor a word. How would we typically do that with no code?

"We have a {word}"
//censored
"We have a {****}"

Do we have a method that can replace words with something else (preferably in a string)?

Well, we want to check if the words in proprietary_terms are in email_two right?
How could we get access to each of the words in proprietary_terms?

What method can give you the index of an element? What method can help you check if there are multiple of an element?

Ask questions!

You’ll find that programming is just a tool to help you accomplish certain things. If you understand what “certain things” is, you won’t be (well, the docs sometimes speak in hieroglyphics) as confused when you go to look up certain methods and figure out how to use them.

2 Likes

So I just realized something, in using the python visualizer that when I use .replace as a print command outside of a function or loop python replaces all instances of that string in the larger string. However when the command is put in a loop it doesn’t work because python is looping through each letter in the string rather than the keyword so it can’t find the string in the larger string.

So is that why we must use indexing to find the position of the string instead?

Okay, so what happens when someone wants to update the list? It seems labor intensive to just manually add .replace and add another . replace.

I was trying to loop though the list of terms and search for each term rather than just manually typing in .replace(“she”," ").replace(“her,” "). replace etc.

I will just ask this in the main thread. Thanks for your help!

Why are you manually typing anything?

for word in list:
  a_string.replace(word, "***")

This is what I wrote:

def function(email):
  return email.replace("she"," ").replace("personality matrix", " ").replace("sense of self"," ").replace("learning algorithm"," ").replace("her"," ").replace("herself"," ")
  

print(function(email_two))

I was curious if this is what I should be writing or if there is a better way.

Oh I see what you are saying now.I think I misunderstood the original set of code.

1 Like

Why doesn’t this loop? I’m confused.

def function(email):
  for word in proprietary_terms:
    x = email_two.replace(word,"*****")
    return x

print(function(email_two))

The return statement will exit the current function and pass a reference back to the caller. Once it executes no other part of the function will continue to run. Perhaps reconsider its location in your code.

It’s also worth taking a close look at the .replace method and what it returns since your function may not behave as you expected right now. More specifically what are you doing with the returned value?

3 Likes

So I’m trying this with a shorter string, I got it to loop with a counter. I have been using the visual python tutor as an aid. I don’t understand why it doesn’t loop to the second word. And I don’t understand how to get it to save the iteration. Does Python store values?

I did figure out that return returns whatever you tell it. I am having trouble understanding indexing and position of values in the string. Is there another resource with more simple exercises that could help me understand these concepts better.

I’ve also been reading Think Python.

sentance = "I can't believe it's not butter. But it is butter!"

lst = ["butter", "it"]

def function(string):
  counter = 0
  for word in lst:
    while counter < len(lst):
      x = string.replace(word,"*****")
      counter += 1
  return x

print(function(sentance))


There’s nothing wrong with redoing exercises you’ve already covered prior to this project. The goal is always to understand them rather than just pass them.

Alternatively break your function down into a few smaller steps. Adding several steps inside a function can blind you to the working of your own code if you don’t understand them completely in the first place. So take them out of a function to begin with and just make sure each step works as it should. At no point should you rely on guesswork.

First get a working code which replaces all instances of a particular substring from an original string and returns the value. Note that the .replace method replaces ALL instances of a substring, not just the first one.

my_str = 'this string is my string'
replace_my_str = my_str.replace('string', 'replacement')
print(replace_my_str)
Out: 'this replacement is my replacement'

Once that’s complete work out a way to do this more than once where the output is altered further each time. Here’s where a loop comes into play.

If you can do that successfully then you can think about moving it into a function. Just ensure each and every step is clear to you. There’s no benefit to performing multiple steps at once if you can no longer follow the flow of your program.

1 Like

I reset the Comp Sci career path thinking maybe I’ll learn something and yes, in redoing the function section something clicked. I guess I shouldn’t be surprised it was on the returns lesson. :smiley: