Censor Dispenser Challenge Project (Python)

Congratulations on completing your project!

Compare your project to our solution code and share your project below! Your solution might not look exactly like ours, and that’s okay! The most important thing right now is to get your code working as it should (you can always refactor more later). There are multiple ways to complete these projects and you should exercise your creative abilities in doing so.

This is a safe space for you to ask questions about any sample solution code and share your work with others! Simply reply to this thread to get the conversation started. Feedback is a vital component in getting better with coding and all ability levels are welcome here, so don’t be shy!

About community guidelines: This is a supportive and kind community of people learning and developing their skills. All comments here are expected to keep to our community guidelines


How do I share my own solutions?

  • If you completed the project off-platform, you can upload your project to your own GitHub and share the public link on the relevant project topic.
  • If you completed the project in the Codecademy learning environment, use the share code link at the bottom of your code editor to create a gist, and then share that link here.

Do I really need to get set up on GitHub?
Yes! Both of these sharing methods require you to get set up on GitHub, and trust us, it’s worth your time. Here’s why:

  1. Once you have your project in GitHub, you’ll be able to share proof of your work with potential employers, and link out to it on your CV.
  2. It’s a great opportunity to get your feet wet using a development tool that tech workers use on the job, every day.

Not sure how to get started? We’ve got you covered - read this article for the easiest way to get set up on GitHub.

Best practices for asking questions about the sample solution

  • Be specific! Reference exact line numbers and syntax so others are able to identify the area of the code you have questions about.

Your solution function censor_four strips out the newlines from the original email when it creates a list with all the words:

def censor_four(input_text, censored_list):
input_text_words =
for x in input_text.split(" “):
x1 = x.split(”\n")
for word in x1:
input_text_words.append(word)

I was trying to figure out how to preserve the newlines in the word list before getting to the censoring code, so that when the text is re-joined after the censoring, it preserves the format of the original email with the newlines . I created two functions, the first to create a list with the email text split up by spaces, same as you, then a second function to parse that list and check each element for newlines (’\n’), and create a new list with the newlines as separate elements:

def text_to_list(text):
text_list = text.split(" ")
return text_list

def separate_newlines(text_list):
words =
for x in range(len(text_list)):
if ‘\n’ in text_list[x ]
i = text_list[x ].find(’\n’)
words.append(text_list[x ][0:i])
words.append(’\n’)
words.append(text_list[x ][i+2:])
else:
words.append(text_list[x ])
return words

I started with looking for one newline per element, and was going to expand on that code to find a second new line. But I found that even though elements in the first list have strings that correctly have the two newlines from email_four, for example ‘HELP!\n\nHelena’, when run through my separate_newlines function it appears to treat the first ‘\n’ as an escape sequence, and my new list has three elements ‘HELP’, ‘\n’,’ Helena’, rather than ‘HELP’, ‘\n’, ‘\nHelena’.

Is that to be expected, and is there a way to correctly recognize the first ‘\n’ as a newline and not an escape sequence?

There are some formatting problems with my post. Indentation is not showing up. Also [x ] shows up as without the extra space I entered. And the square when words is defined is really left and right square brackets.

There are buttons for formatting in the forum post editor

newline is less special than you might think. you can treat it the same as space and punctuation, a non-letter character.

I’m fairly pleased with my end results of this project. (Ignore my poor naming conventions, I would absolutely clean that up for readability but have spent enough time on this project.)

The only thing I would have particularly liked more would be for my censor4 function to put the list back together WITH the original line breaks, but I’ll still take the win.

1 Like

Here was my solution for this challenge. Manages to hit all the objectives with what I feel is fairly concise code–I took an approach of creating 1 function that grew with each objective rather than starting a new one for each task. Only thing I didn’t specifically address was punctuation, but code puts out the results I was expecting. Nice that it also preserves the formatting from the original E-mail, had to split the E-mail first at the line breaks ("\n"), then a second split into words. Now that I type this, I guess a sentence level split would be a good spot to deal w/ punctuation, but at this point I’m ready to move on from this XD

I know it’s a stupid question but…how do we run the code we write in Jupyter? In previous lesson with jupyter, “Reggie Linear Regression” we had “run” button, but now only things I hae is File, Edit, View and language? My “View” button only have Toggle Line Numbers and Toggle Header options.

Why do CC omit something so important?

Edit: I stuck so I looked at the people solutions for a hint. People use stuff that wahe not been covered, like “scrub” (what?).

Is this course really for beginners or did made a mistake and should find some easier course? All of you people seem like someone who already have some experience coding, no way you come with those solutions just after those couple CC lessons :frowning: I feel like a caveman in a library full of people, I mean my code to censor first email is this:

e1 = email_one.replace("learning algorithms", "CENSORED")

When in solution it’s this:

def censor_one(input_text, censor):
  censored_item = ""
  for x in range(0,len(censor)):
    if censor[x] == " ":
      censored_item = censored_item + " "
    else:
    	censored_item = censored_item + "X"
  return input_text.replace(censor, censored_item)

I know all those methods and syntaxes but theres just no way I could come up with something like this, which makes me think that either I’m an idiot or this course just requires something more than just what I gather from this course

Could someone point me to REALLY beginner friendly course? Jumping from 4 line functions straight to over 100 line code is not beginner friendly in my case,.

I think what stops you is perspective, your expectation on how one goes about writing code.

For any task you would consider what input you get and what output you need to produce.
Then, using every-day logic, what steps does that involve?
What is the first thing that needs to happen? Write a few lines for that first thing. Then reassess, what have you now got available, and how do you continue from there, what is the next thing that needs to happen?

It’s an interactive process. Make one small change to bring you a bit closer, test it, look at what you got, and keep going.

Pay special attention to types of things. Both the input and the output, but also your intermediary values. That’s what determines what operations are available. You might need to convert to a different type if you need a different operation. If you have two things that need to be combined in some way, then the two things and the thing you want from it says everything you need to know about the relation/action to make that happen (this would typically be a function, having several inputs and wanting an output)

1 Like

Hey there! Don’t be discouraged by comparing your own solution to the example shared here. My first version of the first censor is pretty much exactly the same as yours!

def censor_word(text,word):
    result = text.replace(word,"####")
    return result

The solution provided doesn’t explain it very clearly, but why their solution looks much more complicated than yours (or mine!) is that they’ve added some additional logic to complete one of the ‘additional challenges’ from task six.

The task reads

Censor words while preserving their length.

So that extra code that looks much more complex than ours is because they’ve also included the solution for the extra challenge at the end. After I got to task six, I also updated my first censor and it came out like this.

### Censor a word or phrase in a piece of text.
def censor_word(text,word):
    censor = ""
    # Make the sensor the same length as the original word (incl spaces).
    for letter in word:
        if letter == " ":
            censor += " "
        else:
            censor += "#"
    result = text.replace(word,censor)
    return result

You’re not alone in the way you’re thinking and approaching the challenges! Think of the solutions provided as a way to inspire you with ideas on how to improve from your initial attempt, not necessarily what your first attempt should look like.

PS. I don’t understand the other two examples posted either, hehe. Don’t worry about comparing yourself to others though. We’ll all keep learning and getting better as we progress.

1 Like

Greetings everyone!

I just finished my project, it doesn’t solve most of the extra challenges, but i thought the suggested paths to solve the challenges were more time consuming then the alternative solutions. I used the identities ‘objectVar’ or ‘objectList’ to name the disposable amorphous variables I used to store track data within the confines a functions scope. other than that my naming conventions are pretty standard and I only used the functions / methods / syntax covered in the python lessons thus far so my code should be highly legible however chunky


instead of listing all instances of ‘REDACTED’ in censoreAll one could just check ( if ‘REDACTED’ in objectLst3[y] ) to the same effect

Cheers!