mardi 3 mars 2020

How do I determine how many parameters to use in a function

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.

Aucun commentaire:

Enregistrer un commentaire