10. censor


#1

Can somebody please tell me whats wrong with my code? When I press run it always returns "[’ ']"

def censor(text, word):
  text_split = " ".split(text)
  for words in text_split:
    if words == word:
      for letters in word:
        letters.replace("*")
    return text_split
      
print censor("this hack is wack hack", "hack")

Your function fails on censor(“hey hey hey”,“hey”). It returns “[’ ']” when it should return " *".


#2

for starters, i would take a look at text_split value after the splitting:

  text_split = " ".split(text)
  print text_split

#3

I found the problem with the split, that problem is fixed, but it’s still not doing what it’s supposed to.


def censor(text, word):
  text_split = text.split(" ")
  print text_split
  for words in text_split:
    if words == word:
      for letters in word:
        letters.replace("*")
    return text_split
      
print censor("this hack is wack hack", "hack")

Your function crashed on (‘hey hey hey’, ‘hey’) as input because your function throws a “replace() takes at least 2 arguments (1 given)” error.


#4

using replace is lame, it allows for a single line solution and it doesn’t censor properly in my opinion. For example:

print censor("this hack is wack hack", "is")

would output:

th** hack ** wack hack

i don’t think this should be partial censored, which is what .replace() does. But there is no test case which covers this

replace takes two arguments:

string.replace(old, new)

old is what you want to replace, and new is what you replace is it with

but apart from that, replace is a method for strings, text_split is a list, so that is confusing, what solution are you going for?


#5

What do you suggest I use other than .replace()?

def censor(text, word):
text_split = text.split(" ")
for words in text_split:
if words == word:
for letters in word:
letters.replace(word, “*”)
return " ".join(text_split)

print censor(“this hack is wack hack”, “hack”)

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


#6

if you do this:

letters.replace(word, “*”)

you replace word with * in letters string, is that what you want?

i would either modify text_split by updating elements in this list using indexes or make a new list and append asterisks to this new list if censoring is needed, else append the current word.


#7

I tried using .append() instead but all that comes out of “this hack is wack hack” is “this”. I haven’t practiced Python in about a week so that’s why i’m having some trouble.

 def censor(text, word):
   new_word = []
   text_split = text.split(" ")
   for words in text_split:
     if words == word:
       new_word.append("*" * len(words))
     else:
       new_word.append(" " + words)       
     return " ".join(new_word)
       
print censor("this hack is wack hack", "hack")

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


#8

here:

new_word.append(" " + words)   

why is there " "?

By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends

so your function ends in the first iteration of the loop, which is why it gives this (first word of text)


#9

It worked perfectly after removing the space that was being appended, and I noticed that the indentation for my return was off so I fixed that. Thanks for the help and fast responses. :grin:

def censor(text, word):
   new_word = []
   text_split = text.split(" ")
   for words in text_split:
     if words == word:
       new_word.append("*" * len(words))
     else:
       new_word.append(words)       
   return " ".join(new_word)
       
 print censor("this hack is wack hack", "hack")

Returns “this **** is wack ****”


#10

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