Need help. Don't understand the problem


#1

So this is my code:

def censor(t,w):
cnsr = "*" * len(w)
t_list = t.split()
for word in t_list:
index = int(t_list.index(w))
if word == w:
t_list.remove(word)
t_list.insert(index, cnsr)
return " ".join(t_list)

print censor('dog cat dog','cat')

but whenever I hit 'Save and Submit' I get this:

Traceback (most recent call last):
File "python", line 11, in
File "python", line 5, in censor
ValueError: 'cat' is not in list

I don't understand why it's saying 'cat'
isn't in the list. if I print the t_list then I get a list containing 'cat'. I'm really lost.
(I have no idea why it doesn't show any indentation on here but I'm pretty sure that's not where the problem is)


#2

Best avoid moving things around in a list that you are currently iterating through unless you really really know what you're doing and probably better avoid it then as well.

It's saying that cause it's not in there. Why that is, is something you have to ask yourself, you are the one who's writing the instructions that it has to follow!

Use print statements to make your code print out what it is doing step by step and see if you can spot where it goes wrong from that.

Regardless of how good your approach is, it's good to follow through with it, to get the machine to do what you imagined. A simpler approach though is to not remove anything from the original, and just add each word that's unaffected just the way it is, and otherwise add the starred out version.