Need help with Censor Dispenser (Question 4)

https://www.codecademy.com/practice/projects/censor-dispenser

I got stuck on question 4.

email_list = []

def negative_remover(negative_word,email):
  negative_count = 0
  email_list.append(email.split(' '))
  for bad_word in negative_word:
    for word in email_list:
      if word == bad_word:
        negative_count += 1
      return negative_count

In this case the negative_count does not increase in the first place. I plan to use the negative_count later on in the function. How should I fix it?

Hello @tag3789977054 welcome to the forums!

Try indenting your return to match your for loop:

for word in email_list:
  if word == bad_word:
  negative_count += 1
return negative_count

Also, do you need this part:

Because, as far as I know, the negative_word is a string, is it not? This means that this loop will parse over individual letters/characters in the string. Also, when you have this section:

What you’re doing is making a new list out of email and then appending that to email_list:

email_list = ["str1", "str2"]
def append_to_email_list(email):
  email_list.append(email.split(" "))
  return email_list
print(append_to_email_list("string1 then string2")

will print:

["str1", "str2", ["string1", "then", "string2"]]

This means that your for loop:

Only looks at "str1", "str2" and ["string1", "then", "string2"] to parse over; it treats ["string1", "then", "string2"] as a single entity, which means that unless you do a for loop looking at characters of the strings in email_list, or indeed the strings in the newly appended list, the count will continue to be the same (0).
For example:

for word in email_list:
  for i in word:
    negative_count += 1

This loop will look at each character in the first few elements of email_list (str1 and str2) but will look at each string in the nested list.
This is just one way, but, for the purpose what you’ve asked, it works.

This is code that works, for the purpose of your question, at least
email_list = ["the", "the", "the", "a"]
def negative_remover(negative_word,email):
  negative_count = 0
  email_list.append(email.split(" "))
  for word in email_list:
      for i in word:
          if i == negative_word:
              negative_count += 1
  return negative_count

I hope this helps!

1 Like