Censor question


#1

Hey i’m working on “Censor” and I don’t know what my problem is.
Can you guys tell me where is wrong in my code?

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

#2

What makes you say that there is a problem? (describe the situation)

Assigning to i has no effect on text2 (no magical connection going on under the hood, i is just a variable like any other)

Considering the difference between what your function does and what it should do should tell you that this assignment is having no effect.


#3

it says that if you enter censor(“hey hey hey”, “hey”)
it returns “hey hey hey” when it should return “*”


#4

Ok. we are here to help each other.
Let’s try to interpret your code line by line.
first 2 lines are good, only the quotes I think are not necessary for now.
line3. You are using i as a counting variable which means it is an integer. 0,1,2 and so on… till the range stops.
line4. So knowing that i is an integer, you cannot compare i with a word.
in the first iteration i = 0 so 0 == word does not make sense.

Let’s see this:

def censor(text, word):
text2 = text.split()
for i in range(len(text2)):
if text2(i) == word: ----- we are trying to access each chunk in text2 by index
text2(i) = ‘*’ * len(word)

return " ".join(text2)

Try it and see if it works. Good luck.


#5

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