Hi folks, I’m running into a problem on the final question of the Censor Dispenser project (https://www.codecademy.com/practice/projects/censor-dispenser) which I can’t seem to get my head around, and was wondering if anyone would be so kind as to take a look at my code.
For context, the issue i’m having is with the following question:
5. “Write a function that censors not only all of the words from the
proprietary_terms lists, but also censor any words in
email_four that come before AND after a term from those two lists.”
I should note that at the minute, the function I am trying to create is just trying to solve a simplified version of the problem by just censoring
email_four of the words in the first list;
proprietary_terms and the words before and after
proprietary terms. I am not yet taking into account punctuation or case. I plan to add the functionality to fully meet the criteria of the question later.
So here’s my (probably exceedingly dry-violating and over-complicated) code (lol):
email_one = open("email_one.txt", "r").read() email_two = open("email_two.txt", "r").read() email_three = open("email_three.txt", "r").read() email_four = open("email_four.txt", "r").read() proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "herself", "her"] negative_words = ["concerned", "behind", "danger", "dangerous", "alarming", "alarmed", "out of control", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", "dismal", "distressing", "distressed", "concerning", "horrible", "horribly", "questionable"] def censor_all(text, censor_list_1, censor_list_2): censor_list_1.sort(key=len) censor_list_1.reverse() censor_list_2.sort(key=len) censor_list_2.reverse() # ^ SORTS THE CENSOR LISTS BY LENGTH IN DESCENDING ORDER for element in censor_list_1: if text.find(element) != -1: index_finder_pre = text.find(element) - 2 while text[index_finder_pre] != " ": index_finder_pre - 1 if text[index_finder_pre] == " ": precensor_start = index_finder_pre # SETS INDEX FOR PRECENSOR START ^ index_finder_post = text.find(element) + 1 while text[index_finder_post] != " ": index_finder_post + 1 if text[index_finder_post] == " ": postcensor_end = index_finder_post + 2 while text[postcensor_end] != " ": postcensor_end + 1 if text[postcensor_end] == " ": text[precensor_start:postcensor_end] = "##CENSORED##" # SETS INDEX FOR POSTCENSOR END, THEN CENSORS ^ if text.find(element) != -1: censor_list_1.append(element) if text.find(element) == -1: print(element) continue # CHECKS IF ALL OCCURENCES OF CENSOR WORD HAVE BEEN REMOVED ^ return text print(email_four) print(censor_all(email_four, proprietary_terms, negative_words))
The problem I’m encountering is one I haven’t really encountered before (perhaps an infinite loop?)
print(censor_all(email_four, proprietary_terms, negative_words)) or calling
censor_all in any way results in a very long loading bar and eventually ends in literally a blank output in the terminal. Not even
[none], I mean no output whatsoever. The terminal doesn’t even print unrelated print statements even if I called a print statement on the first line before any of my code.
What’s weird, is that if i change a variable name to nonsense e.g
457370dfgs#€text anywhere in my function, or for that matter, change any part of the function in a way which should produce an error, the error is found as expected and reported in the terminal. I only get a 10second+ loading bar followed by a blank output when everything in my function seems to be written correctly.
Does anyone have any idea why this might be?