Oops, try again. Did you create a function called censor? Your code threw a "global name 'censor' is not defined" error


#1

I don't understand what I am doing to get this error.
Before it started showing this error I tried replacing the string strWord with a replace(), when I got rid of it to try something it kept on reporting an error about the replace method. I reloaded the page and then I get this error: Oops, try again. Did you create a function called censor? Your code threw a "global name 'censor' is not defined" error.
and I can't understand why before it read the function and not after.


#2

@dodgydago,

def censor(text,word):
    strText = text
    strWord = word
    lista = strText.split()
    for n,i in enumerate(lista):
        if i == strWord:
            count = len(strWord)
            lista[n] = "*" * count
    space = " "
    return space.join(lista)

print( censor("text is text","text") )

============================================

google search
python list replace site:stackoverflow.com


#4
def censor (text, word):
    ast = len(word)
    new = []
    for x in text.split():
        if x == word:
            x = '*' * ast
        new.append(x)
    return ' '.join(new)