15. Practice Makes Perfect



Obviously it would be helpful for someone to explain why this does not work. Also, if you would, please show a more concise way to do this because I've only been coding for a few years (high school), and doubt I'm doing this the best way. Thanks in advance.

Error: list indices must be integers, not str

def censor(text, word):
    string = ""
    words = text.split(" ") #.split() returns a list
    for i in words:
        if words[i] != word:
            string = string + words[i] + " "
            string = string + len(word)*"*" + " "
    return string


In the above, i is a string, not an integer, as required in words[i].


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