Censor


#1



I am stuck at the Censor exercise.

Here is my code:

def censor(text, word):
    for b in text.split(' '):
        if b == word:
            b = '*' * len(word)
    str2 = ' '.join(text)
    return str2


The output is 'h e y, h e y, h e y'


Replace this line with your code.


#2

You are just reassigning the variable b to something else, not doing anything to text. Maybe try a for loop like this... for x in range(0, len(text)), see if you can make that work.


#3

b is just a local variable inside the for loop, it resets each time the loop runs, and cease to exist the moment the loop finishes.

You can use the for in loop like you do, but then you would have to push everything into a new list, and censor it when needs be. Or you could use range() like suggested by @dantemp, this gives you the ability to modify/censor items inside the list.