10. Censor - Wrong attribute type? Methods not for strings?


#1



Error message: "ops, try again. Does your censor function take exactly two arguments, a text string and a word to replace with asterisks? Your code threw a "'str' object does not support item assignment" error."


I found now an easier way to do this using the replace method, but I would like to understand what I'm doing wrong here. Also in general, can I modify parts of strings directly like this "string[x] = y"?

Thanks!


def censor(text, word):
    for n in range(len(text)):
        # for every char in text, check if it equals the first char in word
        if text[n] == word[0]:
            # for next characters up to len(word), check if text[n+x] == word[x]
            # if all of them are, checker remains True, otherwise it becomes False
            checker = True
            for x in range(len(word)):
                if text[n+x] != word[x]:
                    checker = False
                    break
            # if checker ended up True, replace all chars in text[n + y] with "*"
            if checker:
                for y in range(len(word)):
                    text[n + y] = "*"


#2

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