Why am I Getting an Error?


#1

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 + " "
    else:
      final = final + i + " "
  print final
  return final

FAQ: Learn Python - Practice Makes Perfect - censor
#2

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.