"Your code looks a bit off--it threw a "'list' object has no attribute 'replace'" error. Check the Hint if you need help!"

I defined x to be the split string text and y to be the asterisk version of the input word. Then I attempted to replace any word in x with y and rejoin. Any help would be greatly appreciated!!

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


.replace() is a method for string, x is a list.

using replace allows for a single line solution, bit dull while you are trying to learn

we can use indexes to update elements in a list, which function did we learn which gives us the indexes while looping over the list?


Thank you! I have updated my for loop to read

for i in range(len(x)):

but I am still stumped on how to go about replacing i with y


now i will contain indexes, so we can update our list (x)

you learned how to update elements in a list using indexes, right?


I have tried the following code underneath the for loop:
if i == word:
x[i] = y
if i == word:
i = y
if i == word:
x.replace(i, y)
but all return the error "Your function fails on censor(“hey hey hey”,“hey”). It returns “hey hey hey” when it should return “*** *** ***”."
Any suggestions?


this one:

if i == word:
    x[i] = y

is pretty good, except i is index. So we need to use i to get current word from x list, to see if it equals word variable.


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