Censor


#1



10.Censor


Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "" when it should return " *** ***".


How can I return those separate strings in one line?


def censor(text, word):
    for char in text.split():
        if char == word:
            char = '*' * len(word)
    return "".join(char)


#2

In this line nothing happens. char is the iterator variable of your loop and has no ties to data structure (string, list, etc.). Writing the change to this variable has no effect since the loop assigns the next term in the list.

Use a distinct variable whose purpose is to receive assignments, either the term or the star-string.

censored = []                  # initialize an empty list
for term in text.split():
    if term == word:
        censored.append(word)  # append to list
    else:
        # append star-string
return ' '.join(censored)      # join with space separator

#3

Thank you so much!

I correct my code and it works :slight_smile:


#4