Error that shouldn't occur in the censor task


This code works, but the error says it doesn't. What's wrong?

def censor(text,word):
    TextSplit = text.split()
    EndList = []
    Result = ''
    for TextWord in TextSplit:
        if TextWord == word:
            EndList.append("*" * len(word))
    for Component in EndList:
        Result += Component
        Result += ' '
    return Result


Wait, I get it, it's adding an extra ' ' at the end. How do I do it then, with a list of correct components? I can't find anything that helps.


you could just use list slice? for example:

print "list slicing"[:-1]

will slice of the last character, you could do this just before your return. Or, you could use .join() to join the list into a string, this way you don't have to deal with additional space at the end


Thanks! I was trying with more things to check whether the component being added was the last one in the list, but your solution worked best


checking if it is the last one will also work, but slicing is easier and .join() would be the easiest, that is even easier. But like we said, there are multiply ways to solve a problem