10-15 Censor


#1

Hey guys!
Why my code isn't working?

def censor(text,word):
    www=text.split()
    for n in www:
        if n == word:
            www.index(n)=i
            www.remove(i)
            www.insert(i, "*" * len(word))
        else:
            www.index(n)=p
    return "".join(www)

error> File "python", line 5
SyntaxError: can't assign to function call


#2

Take a look at these two lines:

www.index(n)=i
www.index(n)=p

What is the meaning of this code? "Make index of substring n in string www equal to i / p"? Does not make much sense, right? :slightly_smiling:

Order of operations is wrong. You want to store in variable i / p index of substring n in www:

i = www.index(n)
p = www.index(n)

There are two additional logic problems in your code, but hopefully you will be able to fix them without our help :slightly_smiling:


#3

Before you reply i noticed this... kkkkkkkkkkk

in the end the code was like

def censor(text,word):
s=text.split()
for n in s:
if n == word:
i=s.index(n)
s.remove(s[i])
s.insert(i, "*" * len(word))
else:
p=s.index(n)
return " ".join(s)


#4

Just for general knowledge: the method replace does the job in one line. Of course, for practice it is useful to write our own code.
This exercise can be solved also by string slicing.