Censor - Can someone help me understand why my code is off?


#1

def censor(text, word):
---nb = len(word)
---bla = text.split()
---for words in bla:
------if word == words:
--------words = "*" * nb
---text = " ".join(bla)
---print text


#2

Try starting with a variable called index, like this:

index = text.index(word)

then 2 more variables, l and text, then your system should return text


#3

Thank you for your answer. I am not sure though to understand your suggestion?
Why should I use the index function


#4

well, your code is simply off. I shall give you a snippet, and you can probably go from there:

def censor(text,word):
   index = text.index(word)
   l = len(word)

try using that, then having a variable text that is str.replace(text, word, '*' * l), then return text.
If you are satisfied, mark this as the solution


#5

I am trying to avoid using the replace function.
Could you please tell me what exactly is wrong in my code


#6

well, your if statement is unnecessary, plus the variable nb and blah is false. the loop is also unnecessary, you are often overthinking the problem.

P.S. what is wrong with replace?


#7

Not quite clear but thank you.
About replace, I figured I could do without.
Why can't I change the content of the "bla" list replacing word by **** the way I do it? Is it only working ith "replace"?


#8

i'm not sure. I used replace because it was simple to me, but if you can find different ways to fix it that would be good. What is not clear to you? I gave you an idea, what is not clear about it?


#9

Basically: why do I need to create a new list and fill in it ? Why can't I just modify the exisiting "bla" list replacing all the "word" by ***


#10

Yes! Try that! Tell me what happens! There is always more than one way, and come back if you still need help.


#11

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