Practice Makes Perfect


#1

Code

def censor(text,word):
  text.split(" ")
  for idx , item in enumerate(text):
    if item == word:
      text[idx] = '*' * len(item)
  " ".join(text)
  return text

Error

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


#2

here, you split text:

text.split(" ")

you split the string into a list, but don’t store this list anywhere? So text is still a string, so it will loop letter by letter, so item will never equal word

same problem with .join(), you join the list into a string, but don’t store the returned string anywhere?


#3

can i store in the same name list text like this

text = text.split(" ")


#4

yes, now you update the variable containing a string with a list, that is a very good idea :slight_smile:


#5

It works thanks :grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin::grin:


#6