10. Censor - stuck


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/censor

<In what way does your code behave incorrectly? Include ALL error messages.>

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

<What do you expect to happen instead?>
I expect to reach the desired solution, replacing whatever string word is in text with *s.

I am sort of totally lost with this lesson for some reason. I feel like I’m over thinking it though. I turn word into a list and replace each item in the list with *, and then use join. But when I try to iterate through “text” for the word to replace with the variable “word_list” it simply doesn’t work. What am I doing wrong? I don’t want the answer, just a nudge in the right direction, or an explanation of the logic I should be using.

def censor(text, word):
  word_list = []
  for i in word:
    word_list.append(i)
  
  for i in range(len(word_list)):
    word_list[i] = "*"
  word_list = "".join(word_list)
  
  text.split(",")
  for i in range(len(text)):
    if text[i] == word:
      word = word_list
  return text
  
print censor("I am here",  "here" )

#2

Also tried this and still not working.

def censor(text, word):
  x = text.split()
  for i in range(len(x)):
    if i == word:
      x[i] = "*" * i
  return " ".join(x)
      

#3

Did you mean, "*" * len(word)?


#4

Your problem starts on this line

text.split(",")

If i understand well, you want to create a list from a string. So you have to store it in some variable. For instance:

text = text.split(" ")

The second problem is on the same line. You use coma instead of space to create a list. It would be correct if your input was "I,am,here" , but you got spaces between words, so you have to use space (" ") (as in code above) to make a list.
And the third problem is here:

for i in range(len(text)):
    if text[i] == word:
      word = word_list
  return text

So your programm found item in text list that equals to word. It should grab this item and set it equal to word_list, right? But it doesnt grab a list item, it takes a word and set it equals toword_list. So it doesnt affect on text which you return on the next line.

for i in range(len(text)):
    if text[i] == word:
      text[i] = word_list
text = ' '.join(text)

It should looks like this. U found an item, u grab it and set equals to word_list.
And the last thing before u returned a text, you need to get thistext out of list and set it to a string, to get a correct input. Like in the last line in code above.


#6

Thanks a lot for the replies, this code ended up doing the trick after reviewing the replies and several stack overflow articles:

def censor(text, word):
  x = text.split()
  for i in range(len(x)):
    if x[i] == word:
      x[i] = "*" * len(word) 
  return " ".join(x)
      
  
print censor("I am here", "here")

#7

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