Practice makes perfect - censor [10/15]

Hello codecademy.
I am on the practice makes perfect part of the learn Python 2 task. I am honestly having big problems figuring these out, and have resorted to the internet for solutions almost every single time.
This time, I decided I will not do that, so I tried doing it all by myself.
I have currently been stuck for 20-30 minutes, and this is the best I could come up with.
For me, it makes sense, logically, and should make the Word censored.
this is my code so far:

def censor(text, word):
  text.split()
  if word in text:
    text.remove(word)
    text.insert(word, (""*" * len(word)"))
    " ".join(text)
  return text

I split the text into a list.
Then I remove words in the text that is the same as “word”
then I insert the censor * in the place where the word was. where there are as many * as the len of the word.
Then i join the text by a space.
lastly I return the text.

lets actually add a function call so we can see what we are doing:

def censor(text, word):
  text.split()
  if word in text:
    text.remove(word)
    text.insert(word, (""*" * len(word)"))
    " ".join(text)
  return text

censor("hello world", "hello")

now we can actually see what we are doing, lets start with split:

def censor(text, word):
  text.split()
  print text
#   if word in text:
#     text.remove(word)
#     text.insert(word, (""*" * len(word)"))
#     " ".join(text)
#   return text

censor("hello world", "hello")

oops, text is still a string, how come? split() returns a list, it doesn’t manipulate the original string.

one more hint for now: what if word is multiple times in text? You will certainly need a loop

let me know if you need more help :slight_smile: But i thought first i give you one step, and then you can see how far you can get from there.

You need to leverage what does exist, writing something and hoping it’s there and does what you want isn’t going to work. Find out what data type you’ve got and what operations it supports (for example what methods it has)
If the data type you have doesn’t directly support something that you want to do then you may need to use a different data type and then you’ll need to start thinking about how to make that conversion (and there isn’t always a finished way to convert, you may need to take information out of the first value and place it into the other)

There exist different data types for a reason - they’re not the same thing. They have different operations, different shapes, they are not equivalent.

The way you would go about knowing how to use or choose a data type is by knowing a couple of basic ones and what the essence of them is, for example numbers support addition and subtraction along with some other operations. Or if you’re missing information then you may want to start googling to figure out whether something exists that resembles what thinking.

1 Like