I feel like my code below makes sense, but it keeps throwing the error: Your function fails on censor(“hey hey hey”,“hey”). It returns " " when it should return " ". Can someone help point out what I’m missing?

def censor(text, word):
  end_sen = ""
  text_split = text.split()
  word_len = len(word)
  for i in text_split:
    if i == word:
      end_sen += " " + "*" * word_len
    elif i != word:
      end_sen += " " + i
  return end_sen


Manually inserting space characters will result in a different string than the SCT expects.

2 hours later

In an either/or situation do we really need an elif? That would imply a third state.


Good point on the elif. I changed it to an else.

As far as the added spaces, I’ve tried removing them, shortening them, and adding them after, but I still get errors every time. Still not sure what I am doing wrong.


the exercise doesn’t allow leading or trailing spaces, for example censor("hey hey hey", "hey") should give:


i replaced the spaces with underscores so we can see them. This:


and this:


is not allowed. Which makes manually inserting spaces challenging.


Hence why it suggests using the join command. Ok, got it. I’ll give it another try and update with what I get.


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