1

I'm new to Python and have been trying to learn Python from Codeacademy. This is my first post here.

Here is the sample code (answer) given by Codeacademy.

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

def censor_two(input_text, censored_list):
  for word in censored_list:
    censored_word = ""
    for x in range(0,len(word)):
      if word[x] == " ":
        censored_word = censored_word +" "
      else:
        censored_word = censored_word + "X"
    input_text = input_text.replace(word, censored_word)
  return input_text

print(censor_two(email_two, proprietary_terms)) 

Using the above code as an example, how do I know I need 2 parameters? Here input_text, censored_list.

This was the question:

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"]

I had to look at the answer to understand how to solve the question. But for my first try I didn't know how many parameters to put in the function nor could I solve it :[

Is there some rule or some guidelines to follow to know how many parameters a function needs?

For example for the next question:

The most recent email update has concerned Mr. Cloudy, but not for the reasons you might think. He tells you, “this is too alarmist for the Board of Investors! Let’s tone down the negative language and remove unnecessary instances of ‘negative words.’”

Write a function that can censor any occurance of a word from the “negative words” list after any “negative” word has occurred twice, as well as censoring everything from the list from the previous step as well and use it to censor email_three.

negative_words = ["concerned", "behind", "danger", "dangerous", "alarming", "alarmed", "out of control", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", "dismal", "distressed", "distressed", "concerning", "horrible", "horribly", "questionable"]

I think it needs 3 parameters? That is my guess since the previous question required 2 (1 for the email and one for the list of words) and now there are 2 lists and 1 email so I am guessing it needs 3 parameters?

But for other functions and other type of questions.. how do you determine how many parameters it needs? Thanks a lot in advance. I hope my question was clear.

Gojilla
  • 81
  • 1
  • 7
  • 2
    you're spot on with your "guess" of 3. Just count the distinct number of "things" you will need to run the logic, and that's the number of parameters. – Paritosh Singh Mar 03 '20 at 10:36
  • 2
    Have a look at this. It might help you. https://www.programiz.com/python-programming/args-and-kwargs – Reez0 Mar 03 '20 at 10:37
  • 2
    maybe first write code and later try to put it inside function - and you will see how many values you have to send from outside of function to inside of function. Frankly, in most situations number of arguments is so obvious (like breathing) so there is nothing to explain. ie. If I need 3 different elements to create result then I need 3 arguments to send these elements to function. – furas Mar 03 '20 at 11:38

2 Answers2

1

I think you should approach it this way.A function is just some black box that receives some inputs and spits out the desired output. the inputs are the parameters and the output is the the censored text that it returns. let's see an example. let's say you are asked to calculate the average of two numbers. your functions which for clarity you named find_average needs two numbers in order to calculate their average. those are your parameters. if you get confused just draw some random box and think of the output and the list of inputs that are necessarily needed to produce that output. in your last question you are right, you need 3 inputs(parameters) to do what you are asked. your task is to censor a text. now let's list what you need to accomplish that task.

  1. first you need that text to be censored
  2. then you need a the list of negative words
  3. finally you need the list of proprietary_terms

using the above inputs you need to produce one clean censored text as instructed. which means you need three parameters for the specific censoring function.

I hope this clarifies your dilemma.

and a little hint: don't rush to write a function before you understand the problem. you should first solve your problem using some sample input and provided constraints.

1

One way to think about this is "what does the function need to know from the user in order to perform its job?" If a function's job is to print something, then it does not need any parameters. But if it needs to calculate something, it needs to know what to calculate, and what numbers it starts out with. I suggest writing out the function, then adding parameters as you need them. For example, I might write a function for addition like so:

def add():
    *something*

Then, I realize the function needs two numbers to add (let's say a and b). So I add two parameters as so:

def add(a,b):
    print(a+b)

I hope you understand. Comment if you need help.

CheetSheatOverlode
  • 121
  • 1
  • 1
  • 11
  • Thanks, really appreciate your help. Would you be able to give another example? Something that is more complex? – Gojilla Mar 06 '20 at 08:55