10. Censor


#1



Hey guys,

I feel like an idiot that I've spent 3 days on this off and on, but I can't figure this out. This is the function I've created to censor a word. Below are the error messages I get.


Traceback (most recent call last):
File "python", line 21, in
File "python", line 16, in censor
MemoryError


I can't seem to find any pages on these errors when I google them, which was a bit suspicious. Can anyone take a look at this and see where I'm goofing? Thanks. :smile:

def censor(text,word):
    lst_phrase = []
    lst_phrase = text.split()
    space = " "
    censorit = word
    
    for item in lst_phrase:
        if item == censorit:
            item = ('*' * len(item))
            lst_phrase.append(item)
        else:
            lst_phrase.append(item)
    
    return space.join(lst_phrase)
    return word


#2

If you make the list bigger every iteration then you won't ever reach the end


#3

That's fair. So given that, I changed the function to this:

def censor(text,word):
lst_phrase = []
lst_phrase = text.split()
space = " "
censorit = word

for item in lst_phrase:
    if item == censorit:
        item = ('*' * len(item))

return space.join(lst_phrase)
return word

But now I get:

" Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***"."

I can't figure out what I'm doing wrong.


#4

The last place you modified that list in is at

Assigning to a variable that used to refer to something in that list, has no effect on the list. It just makes the variable refer to a new value.


Common to both of those issues is that you just weren't observing what it was doing. How can you do that?


#5

Here's an attempt at changing your approach to implementing your ideas:

Forget about syntax for a little bit and just consider what operations a list supports. Decide which of those you want to use. After that, if you don't know how to do that operation, then look it up - writing something and hoping it'll do what you want will likely not work and may very well do something subtly different if it appears to do what you want. A machine reads the code and executes it exactly, there's no human guessing what you want, right? No guessing.
That may very well mean looking up every single operation you want to do. That's fine, there's no downside to that.

Same thing with guessing what something does vs observing what it does. Don't guess.

If you you look up how to do the things you want and you find out what's going on when it's not behaving as you want, then you'll soon have a much easier time


#6

I hope this program will work. It does to me.
We can use some string function in this program.

def censor(text,word):
    return text.replace(word,len(word) * '*')
print censor("Hey! hey hey", "hey")

#7

That is a very elegant solution.


#8

Wouldn't this solution edit out letters of a partial match though? Say you have "this is that thistle", "is" being the censored - it would then return "th** ** that th**tle". A better example is 'assume' having a profanity in its spelling but you wouldn't want to return "***ume" if you were editing for the profanity(another word for donkey)


#11

This is the easiest solution:


#12

you could do away with the for loop if you define asteriks as being "*" * len(word)


#13

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