Censor exercise


#1



Link to the exercise I am stuck at: https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#

It says this:
Oops, try again. Your code looks a bit off--it threw a "'str' object has no attribute 'append'" error. Check the Hint if you need help!


def censor(text, word):
    censored = "*"*len(word)
    for character in text.split():
        if character == word:
            censored.append(word)
    return "".join(censored)


My logic: I define function, then add however many * signs are needed for the length of the censored word, then loop through and append the * to the censored character. Then it returns the word with the newly changed characters.


#2

from the instructions:

censor("this hack is wack hack", "hack")

text is a sentence, you need to loop over all words to see if they match word.

append() is a method for appending to list, not to string


#3

but I need to append to a string, why can't I? How else are you supposed to replace words once you've looped through and seen that they match?


#4

you can use +:

"hello" + " " + "world"

think about how you want to implement this


#5

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