Censor


#1

def censor(text,word):
    text =  text.split(" ")
    word = word.split(" ")
    for item in word:
        word.remove(item)
        word.append("*"*len(item))
    for thing in text:
        if thing in word:

            text.remove(item)
            text.append("*"*len(item))
            
            
    return word

the problem is the return i have done i know but what should i returned it exactly
error:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "['']" when it should return " *** ***".


#2

Hi
I am not sure that your first loop makes sense because you are supposed to modify text and not word. So I would first delete the lines

word = word.split(" ")
for item in word:
    word.remove(item)
    word.append("*"*len(item))

Then I don't recommend to modify the object on which you are iterating. I would instead create an empty object before the loop and append the ***** or the current word to it depending on if the current word is censored or not.

Finally, don't forget that you must return the modified text and not the word to be censored.


#3

def censor(text,word):
    Eminem = [" "]
    for item in text:
        Eminem.append(item)
    for thing in word:
        for kalo in Eminem:
            if kalo in thing:
                Eminem.remove(kalo)
                Eminem.append("*"*len(kalo))
    yossef = " ".join(Eminem)
    return yossef

no you dont get it this is my problem after last edit sorry for not doing it early
error:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns " * * * * * * * * *" when it should return "*** *** ***".


#4

@letmeprogramyou1 here have my code and execute it if need any help feel free to tell me :wink:

def censor(text, word):
lst = text.split()
for i in lst:
if word in lst:
loc = lst.index(word)
lst.remove(word)
lst.insert(loc, "*" * len(word))
newLst = " ".join(lst)

return newLst

just take care of the indentation and mark this as a solution if satisfied :smile:


#5

no i got it from your code thanks


#6

@letmeprogramyou1 even after copying the code it did not get executed??
Because for me the code worked perfectly..


#7

Got it with some help in this forum

def censor(text, word):
    lword = word.lower() #to lowercase
    lst1 = text.lower() #to lowercase
    lst = lst1.split()
    
    for i in lst:
        if lword in lst:
            loc = lst.index(lword)
            lst.remove(lword)
            lst.insert(loc, "*" * len(lword))
    newLst = " ".join(lst) #Space between quotes important. Was getting ******etc before space. Now I get *** *** ***
    print newLst #Not needed for exercise
    return newLst
    
#Not needed for exercise
cen_a = raw_input("Enter some text: ")
cen_b = raw_input("Enter a word that you would like censored from your text: ")

censor(cen_a, cen_b)
print newLst

#8

you got me wrong i got it from your code i passed the exercise sorry for that "no" i wrote it in the begaining and left it and thanks


#9

I used a regex substitution. Much more concise code and you can forget about the loops. This linked helped me:


#10

It's working.

def censor(text, word):

word_l = word.lower()
text_l = text.lower()

text_sl = text_l.split()
text_so = text.split()
index=0
for i in text_sl:
    if i == word_l:
        text_so[index]= "*" * len(word_l)
    index+=1

newText = " ".join(text_so)
print newText
return newText

print censor("123 234 123 dasdf 123", "123")


#11

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