#10: Made Two Scripts. Both Seem to Work but Aren't Accepted


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/censor-?action=lesson_resume

Hi guys, I am having trouble with this lesson. The first script is not being accepted even though it seems to censor all the words as per instructions. I thought it wasn’t robust enough, so I scrapped my first script and created another one. The second one is also not accepted. Is it because I’m not utilizing .join() ? Or are my scripts just really poorly made? I’m currently trying to find other methods on making this function work.

def censor(text,word):
  word2 = word.lower()
  bleep = "*" * len(word2)
  text2 = text.lower()
  textlist = text2.split(" ")
  rightthink = []
  for items in textlist:
    if items != word2:
      rightthink.append(items)
    else:
      rightthink.append(bleep)
  result = str(rightthink)
  print result
  return result
   


#----------------------------------------

def censor2(text,word):
  word2 = word.lower()
  bleep = "*" * len(word2)
  text2 = text.lower()
  textlist = text2.split(" ")
  rightthink = ""
  for items in textlist:
    if items != word2:
      rightthink = rightthink + " " +items
    else:
      rightthink = rightthink + " " + bleep
  print rightthink
  return rightthink



#2

The first thing to do is write a function that does not manipulate the original text. In other words, no case conversion.

text is a value that may contain spaces, so to remove them, convert it to a list.

text = text.split()

It’s not important that we keep the original phrase, just the words it contains. We do not need to clone word.

 bleep = len(word) * '*'

In a sense we don’t need that either. Think in terms of bare-bones, and set robust aside for the time.

Now we build a temporary list of just values and join them with a space separtor in the return statement.

result = []
for term in text:
    if term == word:
        result.append(len(word) * '*')
    else:
        result.append(term)
return " ".join(result)

That will work on the original text and censor word without manipulating either. The exercise does not ask us to write case insensitive code, so Hey and hey should be treated as different from one another, and only the hey censored if word is hey.


#3

Thank you so much for your help!
I actually found a far, far simpler answer to get through this exercise than my previous lines of code.

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


#4

Which you “found”, and didn’t come up with yourself. Your future empoyers will love you when they find this thread. It’s the internet, after all.

You want to use code you do not understand, that’s up to you.


#5

Hello! I appreciate that you were concerned and thought that I gave up on the problem and googled a solution. I probably should have been a little more clear than using the word “found”. Here is how I came up with this code:

  1. I discovered the .replace() method a while back. You can see myself struggling with this method here: #8 AntiVowel : .Replace() Problems

  2. I remembered how useful that method was so I made a code somewhat like this:

def censor(text,word):
  text2 = text.split(" ")
  bleep = "*" * len(word)
  for items in text2:
    if items == word:
      result = text2.replace(items,bleep)
  print result
  return result
  1. Python threw an error that I was using a .return() method on a list. So I made this code. It passed:
def censor(text,word):
  bleep = "*" * len(word)
  result = text.replace(word,bleep)
  print result
  return result
  1. Then I saw your comment on my “bleep” variable. So I made the code a little neater. Now we have
def censor(text,word):
  result = text.replace(word, "*" * len(word))
  print result
  return result

I did use the internet which led to my discovery of .replace() (and most recently filter() ) on Stackoverflow, but rest assured I did not do a copy/paste job! Next time I post on the forums, I will write down my thought process on how I write these scripts.


#6

One cannot criticize initiative and determination, which is something a self-learner needs in spades, so kudos for all the extra effort to research solutions. I hope that in the process you also learned why we should delve into how something works with lots of experimentation and reading before adopting it as a solution. That is my only concern.

Having the right answer is no where near as important as knowing why it is the right answer, and whether or not it is the best solution for a given scenario. As you will be discovering in learning any language, not just Python, there are many ways to skin the cat, so to speak, and not all solutions are equal, nor any one more superior than the others. Each will have advantages and disadvantages which need to be weighed in the context of the problem being solved and/or the purpose of the exercise.

Do you know why the last solution works so splendidly? Can you still come up with other solutions as well that do not use a built-in to do the heavy lifting? If not then what you have essentially done is toss the baby with the bath water.

Don’t settle on your solution because it is the best. Satisfy your own curiosity by writing several that employ different approaches and solve it algorithmically. Then you won’t just carry away one working solution, but will have learned how to approach a problem from several angles. That is how you build a real skillset.


#8

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