10/15 Something went wrong :( Click Close to try again or refresh the page Error not solving


#1

I am repeatedly getting this error when i run this code:

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

I have tried everything, from rebooting my browser, to clearing the cache to logging out and in again and it won't go away. Any suggestions?


#2

@samskrit,

def censor(text, word):
    censorWord = ""
    wordLength = len(word)
    for x in range(0,wordLength):
        censorWord += "*"
        #print censorWord
    #print censorWord
    tlist = text.split()
    for index,x in enumerate(tlist):
        
        if x == word:
            #print(index,x)
            tlist[index] = censorWord
            #print(index,x)
#            x = tlist.append("*"*len(word))
#        else:
#            tlist.append(word)
#    return " ".join(tlist)
    print("Original text : "+text)
    text = " ".join(tlist)
    print("Censored text : "+text)
    print(text+" <==> "+str(tlist))
     
censor("i make a sentence","make")

[output]

Original text : i make a sentence
Censored text : i **** a sentence
i **** a sentence <==> ['i', '****', 'a', 'sentence']

#3

@samskrit,

import re

def replace_all(str,repls):
    repls={repls:"**"}
    # return re.sub('|'.join(repls.keys()), lambda k: repls[k.group(0)], str)
    return re.sub('|'.join(re.escape(key) for key in repls.keys()),
                  lambda k: repls[k.group(0)], str)


text =  "i make you of all makes"
print replace_all(text, "ak")  #[output] i m**e you of all m**es

#4

Guessing you've already got this, but I think you need a new list. You can't rewrite to your tlist because you're already using it.
This seems to work:

def censor(text, word):
    tlist = text.split()
    new = []
    for n in tlist:
        if n == word:
            n = "*" * len(n)
        new.append(n)
    return " ".join(new)`

#5

is me coding

 def censor(text,word):
        word_new = "*" * len(word)
        str_new = text.replace(word,word_new)
        return str_new