10/15 Censor Questions


Hello, regarding the Censor exercise:

My code seems to censor correctly when I test it (it replaces the censor word with asterisks), but I get the error message ‘Your function fails on censor(“hey hey hey”,“hey”). It returns “_" when it should return "”.’

To be clear it seems to be saying my function should return an empty space, but it is instead returning three spaces in a row. The auto-formatting changes the exact quote.

I don’t understand; I thought the function would be supposed to return “*** *** ***” in this case rather than an empty space. What am I missing?

Here is my code:

On a side note, even if my code is actually “correct” I feel that it’s very awkward and probably longer than necessary. If anyone has any general advice as to how to make code shorter and more elegant, I would really appreciate it because nearly everything I write ends up looking like this. :frowning_face:

Thanks to anyone who replies!


for example:

censor("hey hey hey", "hey")

your function returns:


i replaced the spaces with underscores, which allows us to see that you have a trailing space at the end, which shouldn’t be

for converting string to list there is a built-in function for example, that would shorten the code a lot


You can fix what stetim94 pointed out by erasing the for loop in out = “” and just going with

#converts censoredList back into string 
  out = " ".join(censoredList)
  return out

This is my code if you want to look at it, I did it a little bit different https://repl.it/@fedexm/RewardingDimgraySupercollider

Im just starting just like you so I cant help you that much with simplifying your code, but it may help you to learn more functions like .split(), .join() https://docs.python.org/2/library/stdtypes.html?highlight=join#str.join


I actually wanted let metaquack figure it out on its own as much as possible.

as for your solution, you could define stringed before your first for loop, so you don’t have to redefine stringed each iteration of the loop


Oh, thank you for this advice. I didn’t see the trailing space at the end, but that’s probably the problem.


Thank you for replying.

I agree I should focus more on becoming familiar with the existing functions built-in to Python. I’ve realized probably the reason my code seems so clumsy to me is because I am putting too much effort into “re-inventing the wheel” instead of using the tools that are already there.


it is difficult to see :wink: that is the problem

depends, being able to come up with an efficient algorithm and one you understand can be very valuable.


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