Censor


#1

I get this error message
Your function fails on censor(“hey hey hey”,“hey”). It returns “***” when it should return “*** *** ***”.


def censor(text, word):
  text.split(word)
  item = word
  if item == word:
     item ='*' * len(word, )
     " ".join(text)
  return item



#2

Found in https://docs.python.org/2/library/stdtypes.html#string-methods
str.split([sep[, maxsplit]])
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
Thus if you want to split the text into separate words
you will have to use the =space-character= " " separator
like
text.split(" ")
This split() method will return an Array/List of words which you should capture in a variable
like
text_in_words = text.split(" ")
You could have made your function more verbose so you would see what you are actually doing
like

def censor(text, word):
  text.split(word)
  print text
  item = word
  print item
  if item == word:
     item ='*' * len(word, )
     print item
     " ".join(text)
     print text
  return item

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

#4

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