15. Practice Makes Perfect


#1



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


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] + " "
        else:
            string = string + len(word)*"*" + " "
    return string


#2

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


#3

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