Learn Python: Censor lesson - why is this code considered incorrect?

This question is regarding the lesson Censor in the Learn Python 2 module:

I am told that my code fails for the phrase censor(“hey hey hey”,“hey”), but it appears to work properly (here and on other phrases I’ve tested). I do not use the " ".join(list) command, but I do not understand why this is necessary / what it does better.

My code is as follows:

def censor(text,word):
  x = text.split()
  bleep = "*" * length
  temp = ""
  for w in x:
    if w==word:
      temp = temp + " " + bleep
      temp = temp + " " + w
  return temp

print(censor("hey hey hey","hey"))

Any help is appreciated - thanks!

1 Like

Are you also told in what way it fails?
And when you say it doesn’t appear to fail that way, how are you testing that? I see that your code includes calling your function for that input, but how do you compare the output to what it’s supposed to be?

str.join is not required for a correct implementation, but it might be required for a solution that carries out an appropriate amount of work (unless you manage to reimplement it somehow, but it is not obvious to me how you would)

An appropriate amount of work here is to spend a constant amount of time per character, but you end up creating quite a lot of intermediary strings (what is the total length of those strings relative to the length of the input?)

1 Like

copying and pasting from the error, I am told:

Your function fails on censor("hey hey hey","hey"). It returns " ***" when it should return "*** ".

It looks like there is an extra space in mine, according to the error message. But I don’t see that in the output (there’s just a single space between the bleeped words). This makes me think that str.join inserts something other than a space, but it still looks like a space. I think it’s likely that the answer to my question revolves around this command.

1 Like

I expect it is in the output, or if it really isn’t, that whatever displays it does so incorrectly.
Have you for example tried highlighting the output with your mouse? The space would be easier to spot that way. Or, if printing another line as well without a leading space, that line would be slightly further to the left.
You can print it out with quotes surrounding it so that the space shows up more easily.
You can also print its length, or use == to compare.
And, if you read your code, is it going to add a space at the front?
Why are you saying str.join inserts something else? Are you saying that the supposedly correct result has some non-space spaces in it? If so, that would be measurable, wouldn’t it? It doesn’t explain your leading space, does it? Or if you mean that the leading space in your own result, how would str.join be responsible for that when you’re not using it?



I see - yes, it is in the output. I was focused on later ones, and the difference is that my code adds a space at the start when the first word is censored. This leading space is what I overlooked, and I think what str.join() takes care of. Thanks!