10. Censor


#1




I am getting the error message:
Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey hey hey" when it should return "*** *** ***".


The code is supposed to censor a word and replace it with asterisks. I'm fairly new to Python, and I am not sure what I did wrong.


def censor(text, word):
    word = word.split()
    final = []
    for i in word:
        if i == word:
            final += "*" * len(i)
            return text.join(final)
        else:
            return text
            
print censor("hey hey hey", "hey")


#2

Hi, @geckosrock ,

Here, you have overwritten the function parameter, word, with the result of applying split() to word...

word = word.split()

Instead, you need to divide up text, which contains the words that need to be tested. You can do this as follows, placing the words into a words list ...

words = text.split(" ")

Now, you can iterate through words ...

for i in words:

Inside the loop, you have a test ...

if i == word:

If i and word are equivalent, append the censored word, as asterisks, to the final list ...

final.append("*"*len(word))

... but do not return anything, just yet.

If, on the other hand, i and word are not equivalent, append the actual item to the final list, still not returning anything ...

final.append(i)

Then, after the loop has completed all its iterations, put the items in the final list back together and return the result ...

return " ".join(final)

Make sure to get the indentation right on the return statement, so that it is not part of the loop.

Let us know how it works out.


#3

Thank you so much!!! I did everything you said, and now the code works perfectly! Thanks for spending the time to figure out the problems! You guys are awesome! :slight_smile:


#4

One quick question: What do the quotation marks do in these two lines?

words = text.split(" ")

return " ".join(final)

Are they maybe adding spaces between the returned asterisks?


#5

" " is a space character.

The first of the two lines you have posted splits up the string that is referred to by text, splitting it wherever there is a space, and assigns the parts to words, as a list. The spaces are not included in the list.

The second one joins together the strings that are in the list referred to by final into a single string, with a space character between each adjacent pair of the joined components.


#6

That makes a lot of sense! Thank you for the explanation. :slight_smile:


#8

so , i found that str.replace() is pretty handy

def censor(text,word):
    result = ""
    if(word in text):
        result = text.replace(word,"*" * len(word))
    return result

#9

Oh wow! I had no idea that function existed! Thanks for the info! :slight_smile:


#11

hey can you tell me what do we do to join the items in list without keeping spaces between them...?


#12

@appylpye already covered this? the character between the quotation marks it what joins is what join() uses to join the string. so for example if you want a hyphen between the words:

"-".join(text)

but if you would have read appylpye answer, you could have known this already


#13

@abcoder1

I often visit the Official Python Web Site to look at their documentation, in order to learn about particular details about methods, and to casually browse for other information that may be useful at a future time.

See the documentation regarding str.join(iterable) for Python 3. It also applies to Python 2, which Codecademy still teaches.

Another good place to go on a regular basis is Stack Overflow. There, users discuss lots of different topics regarding programming, and often probe interesting details. For an example, see Python join, why is it string.join(list) instead of list.join(string)?.


#14

I did it in this way is it better(faster when ran) compared to different logics given here or BAD :slight_smile:

def censor(text,word):
list1=text.split(" ")
for n,x in enumerate(list1):
if x==word:
list1[n]=""len(word)
return " ".join(list1)


#15

yes, picking a good approach will improve the running time. People who are new to programming sometimes struggle with passing the exercise, they are not yet ready to optimize.

even your version could be topped


#16

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

HELLO guys can u help me thx!!!


#17

your split:

words = text.split("")

i would split by spaces (" ")


#18

there is a problem with this solution if for example you pass (There is no world like ours but worldwide economy is even worse, world)
You will get
There is no ***** like ours but *****wide economy is even worse


#19

Yes, it is best to split the text by spaces, then work with the resulting list to censor individual words.


#20