10/15 -- censor getting started hint/help


#1

I’m not really even sure where to begin – I know what I’ve done isn’t right but I’m struggling to understand the best way to substitute multiple things in a row for censor, lesson 10/15

If at all possible, give me a hint or an idea about the best approach, rather than a solution. :slight_smile:

Thanks!

What I have so far, and which does not work is:

def censor(text, word):
  text.split()
  for x in text:
    print x
    if x[0:len(word)-1] == word:
      print x[0:len(word)-1]
      x[0:len(word)-1] = "*"
    " ".join(text)  
    return text


#2

here:

text.split()

you split the string, but do nothing with the returned result of the split action?

splitting is certainly a good way, this means we can loop over the words in text, making your job a lot easier


#3

OK! So – I figured out how to get it to semi work – as long as there is only one instance of the bad word.

def censor(text, word):
  text2 = text.split(" ")
  print text2
  badword = text2.index(word)
  text2.insert(badword, "*"*len(word))
  " ".join(text2)  
  return text2

So I’ve modified it to the below, but I’ve got something mucked up – there’s something wrong with the if statement in my loop, maybe? It appears to be doing it right but the system says it’s wrong.
Your function fails on censor(“hey hey hey”,“hey”). It returns “[’’, ‘’, ‘’]” when it should return " ".

def censor(text, word):
  text2 = text.split(" ")
  print text2
  bleep = "*" * len(word)
  for x in text2:
    if x == word:
      badwordat = text2.index(x)
      text2.insert(badwordat, bleep)
      text2.remove(x)
  print " ".join(text2)
  return text2

#4

Got this one solved too – realized the return needs to be the print.

Thanks!


#5

nicely done, solving the problem yourself :slight_smile:

you could also consider using range() in your loop, then you can use the indexes generated to replace elements from text2 list

but then in the if condition, you also need to use the index to get current word from list

if you are truly lazy, use enumerate (its a built-in function), which gives both indexes and value from the list, you can google that :wink:


#6

Thanks! Feels good when I finally figure out the basic mistakes I’m making :slight_smile:

I’ll definitely check out the enumerate function more closely, since I will definitely feel lazy at some point!


#7

start by using range() in your for loop, and then also attempt enumerate. Its good practice to solve an exercise different ways.


#8

My code is similar, but different from arrayblaster70930’s. I’m trying to make mine slightly more efficient, but it isn’t working. Can someone provide a hint as to why? And is there a more efficient way to accomplish this task?

The code is throwing a “‘spam’ is not in list” error.

Here is my code:

def censor(text, word):
  list = text.split()
  star = "*" * len(word)
  for badword in list:
    if badword == word:
      list.remove(badword)
      word = list.index(badword)
      list.insert(badword, star)
  return "".join(list)

Thanks!


#9

if you first remove the bardword, and then try to retrieve the index, its not working. For two reasons: given you just removed badword, it will find the next instance, so it will skip the first one

when removing the last badword, and then after that trying to use index won’t work, given its no longer in the list.


#10

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