10/15 - Censor "an integer is required" error


#1

Hi, can you help me see what’s wrong with my code? I’m getting an “an integer is required” error. Also, is there a more efficient way to accomplish the task in this exercise?

Here’s my code:

def censor(text, word):
  list = text.split(" ")
  star = "*" * len(word)
  for badword in list:
    if badword == word:
      word = list.index(badword)
      list.insert(badword, star)
      list.remove(badword)
  return " ".join(list)

Thanks!


#2

Not a good practice to use reserved words in your functions. list is a built in function.

words = text.split(' ')

You should not be overwriting the parameter variable value.

for term in words:
    if term == word:
        term_index = words.index(term)
        words.insert(term_index, star)
        words.remove(term)

#3

Thank you! It works now. Would you mind explaining why overwriting the parameter variable value caused the error? I’m not sure I understand why that’s a problem. Is the (rough) idea this: reassigning ‘word’ to the index assigns it to an integer, and then when the function tries to run back through with ‘word’ as an input, it is looking for an integer since that is what I’ve assigned it to, but codecademy has the exercise set up so that the function is only being fed strings?

Thanks again. And thanks for tips regarding best practices too.


#4

The word variable in the parameters is meant to be constant so its value remains unchanged through the full iteration of the loop. It is the word we are looking to censor out of (redact from) the provided text block. It is not a Codecademy dictate, but a requirement of the censor function in order to ensure successful execution.

By definition, both inputs are strings. This is a design constraint built around what we expect the function to do (string operations) not a Codecademy ‘set up’, as such. This is a Practice exercise in which we are free to explore various approaches and methods, but whatever we write it must succeed by censoring the word (all occurrences) from the text. This goes to design fulfilling the expectations.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.