Why am I Getting an Error?


Can someone please explain how this code is wrong? It puts out the output: (the password is ****
*** *** ***)
So why am I getting this error?
[Your function fails on censor(“hey hey hey”,“hey”). It returns " 3 asterisk"
when it should return "3 asterisk ".]
edit: I had to put in the words 3 asterisk as the reply wouldn’t show them. There is a space after the asterisks it says I should have gotten.

Makes no sense to me

def censor(text, word):
  final = ""
  lst = text.split()
  for i in lst:
    if i == word:
      i = "*" * len(i)
      final = final + i + " "
      final = final + i + " "
  print final
  return final

FAQ: Learn Python - Practice Makes Perfect - censor

lets replace the spaces in your censored string with underscores:


as you now can see, you got a trailing space at the end which shouldn’t be.