Throwing problem despite giving accurate answer in censor


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


I don't know what is wrong in my code. It is giving the absolute answer but compiler throwing up error which is very vague.



def censor(text, word):
    l = text.split()
    text = ""
    censored = "*"*len(word)
    for i in l:
        if i == word:
            text +=censored
        else:
            text += i
        text += " "
    return text.


#2

you have an additional space at the end of your sentence, lets replace the spaces with underscores so we can see it:

***_***_***_

the easiest thing to do would simply to slice the last character of using string slicing.

the better thing to do would be to make text a list, then append to text and use .join() in the end to join the list into a string

Strings are immutable in python where as list are mutable, so your solution requires more memory


#3

Thanks for your sound and clear explanation. :slight_smile:


#4