Censor


#1



censor 10/15
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "['the password is spam', 'hey hey hey']" when it should return "*** *** ".".

what is my computer interpreting this as
what is the correction and how does the computer interpret it


mylist=[]
def censor(text,word):
    block_out=list(word)
    text1=list(text)
    x= word in text1
    if x ==True:
        mylist.append("*"*len(word))
    else:
        mylist.append(text)
    return mylist


#2

The list is in global scope and the function cannot access it. Define the list inside the function.

Plus you need a loop construct to iterate over the text. You won't need to make a list from word, just leave that variable as is and use it for comparison. Also, you need to split() the text so you can examine each word separately.

Study this example:

def censor(text,word):         # text will be a phrase; word will be a single word.
    text = text.split()        # text will now be a list of words
    result = []                # an empty list to store the results
    for x in text:             # x will be one word at a time
        if x != word:
            result.append(x)
        else:
            result.append("*" * len(word))
    return " ".join(result)     # the return value will again be a string