Censor


#1



error message:
Traceback (most recent call last):
File "python", line 11, in
File "python", line 8, in censor
AttributeError: 'NoneType' object has no attribute 'remove'


am I using .remove() and .insert() incorrectly??
-________-

def censor(text, word):
    lst= text.split()
    number= lst.index(word)
    replacement= "*" * (len(word))
    for each in lst:
        if word==each:
            lst= lst.insert(number,replacement)
            new_list= lst.remove(word)
    return" ".join(new_list)
        
print censor("hey hey hey", "hey")


#2

this line:

lst= lst.insert(number,replacement)

.insert() will update the list for you, no need to store result of calling the method in a variable

more detailed:

.insert() will manipulate the list directly, but the method doesn't return anything. So, the default is returned (which is None for all function and methods), which you store in lst by doing lst=

so now your list is gone, and lst contains None.

which also explains the error message, None(type) has no attribute remove.

A list has remove() attribute, None doesn't


#3

okay got it.
so if I changed that line and the next one to:
lst.insert(number,replacement)
lst.remove(word)
I still get an error:
Traceback (most recent call last):
File "python", line 11, in
File "python", line 9, in censor
NameError: global name 'new_list' is not defined

further, without the last line (print ....)
I get None...


#4

can you post an updated version of your code?


#5

yes, sorry:

def censor(text, word):
    lst= text.split()
    number= lst.index(word)
    replacement= "*" * (len(word))
    for each in lst:
        if word==each:
            lst.insert(number,replacement)
            lst.remove(word)
    return " ".join(new_list)

#6

here:

return " ".join(new_list)

new_list no longer exist? (you removed it), if your inserting and removing goes right, you should be to join and return lst


#7

ahhh okay, right right. so I changed it to lst
and I get the error message
Oops, try again. Your function fails on censor("Yo go fro yo go","go"). It returns "Yo ** ** fro yo" when it should return "Yo ** fro yo **".


#8

^, i set a condition for that to work

the problem is that index() will always give you the first match, so how are you going to find second or third word which requires censoring?

possible: after removing, call index() again to get the next word that require censoring


#9

ahhh I completely overlooked that aspect somehow!!!!!
I thank you soooo much!
I moved the index line directly under the "if" line and it worked!
Thank you for your time and patience with me!!
<3


#10

sorry to bump in, but even if i use the same code as in here, all my whitespace is gone. so something like "hey hey hey","hey" gets turned into ********* and not *** *** ***.
anyway to fix this?


#11

without your code, its impossible to tell what causes the problem


#12

"same code as in here" - but i will make a fresh post. I don't want to occupy this thread. I hope you will give me a hand there. Thanks


#13

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