Censor. Any of it any good?


#1


Is there anything thats good here?


text = "this hack is wack hack"
word = "hack"

text = text.split()
word = word.split()

def censor(text, word):
    lst = ""
    for i in text:
        if i == word:
            lst += ("*" * len(word))
        else:
            lst += word
    return lst


#2

for validation only the function is called, not the rest of the script. So i would place this:

text = text.split()
word = word.split()

why do you split word? this:

"*" * len(word)

works fine when `word is a string.

finally, here:

lst += word

word contains the word you want to censor, is this really what you want to add to lst if the word isn't the word that needs to be censored? (does that sentence even make sense)

so yea, there are some good bits. Just a few bits which need improving as well.


#3

also take a look at this:

# function declaration
def censor(text, word):
    print text
    print word

#function call
censor("this hack is wack hack", "hack")

see how the arguments i supply at the function call are passed into the function parameters (parameters are defined at the function declaration)

That should make live easier :slight_smile:


#4

i understand. Here is my update. I thought that in order to compare, both needed to be lists.

text = "this hack is wack hack"
word = "hack"

text = text.split()

def censor(text, word):
    lst = ""
    for i in text:
        if i == word:
            lst += ("*" * len(word))
        else:
            lst += i
    return lst

this results in: this****iswack****

How can I insert spaces? Its got to do with the "".join(list) example?


#5

you could add the spaces after each word:

lst += ("*" * len(word)) + " "

same for uncensored words. This however represents you with a problem, because it results in this:

this_****_is_wack_****_

i temporary replaces the spaces with underscore, so we can see the additional space at the end of the string. you could slice this off, this is a possibility

But as we already determined, strings are immutable in python so a better approach to make lst into a list, it is currently a string

then use append() to append the (un)censored words to lst

then use " ".join(lst) at the end, then your code is: and more efficient, you don't have to deal with the additional spaces at the end, join takes care of this for you


#6

I got it. BUT there's an i get an error on hey hey hey. although the result looks fine to me.

text = "hey hey hey"
word = "hey"

text = text.split()

def censor(text, word):
    lst = []
    for i in text:
        if i == word:
            lst.append("*" * len(word)) 
        else:
            lst.append(i)
    return " ".join(lst)

#7

we talked about the exercise only calling the function for validation purposes, not the whole script, right?

so this line:

text = text.split()

should be inside the function, then your function works for any list you pass as argument in the function call


#8

def censor(text, word):
    text = text.split()
    lst = []
    for i in text:
        if i == word:
            lst.append("*" * len(word)) 
        else:
            lst.append(i)
    return " ".join(lst)

Thank you so much. I have one more question though.

I know it works but what does this do?

return " ".join(lst)

How does the function know where to place the " " ?


#9

.join() is a built in function which joins the list into a string

I have no idea how .join() works under the hood, maybe you can find it


#10

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