Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List Censor Thread
#1
I am making a program for my Introduction to Computer Science course where I have to type in a sentence: "Dr Mathias is completely full of shirt I wish that jumping papaya would eat some macaroni" and censor the words: "papaya, grasshopper, jumping, shirt, macaroni".

I was able to make a program that would censor words that were inputted, but I am not able to fix it to where it would automatically censor words that are put into a list, without having to input anything. This is where I have gotten so far, when I try to do it I am not getting any output.


#program to censor a string


#input sentence and which words to censor
originalsentence = raw_input("Enter the text to censor: ")
censoredwords = ["papaya","grasshopper","jumping","shirt","macaroni"]


censor=""
wordattempt=[]
wordlist="".join(censoredwords)
counter=0

#apply censor to words typed in
for character1 in str(originalsentence):
    for character2 in censoredwords:
        if character1 == wordlist[counter]:
            wordattempt.append(character1)
            censor=censor+"*"
            counter=counter+1

            #print censored version of word
            if "".join(wordattempt)==censoredwords and counter == len(censoredwords):
                print originalsentence.replace(censoredwords,censor)
                censor=""
                wordattempt=[]
                counter=0
Any help is appreciated. Thank you

User has been warned for this post. Reason: Not posting code in code tags
Reply
#2
Rather than trying to do this character  by character, split the input and do it word by word:

originalsentence = raw_input("Enter the text to censor: ")
censoredwords = ["papaya", "grasshopper", "jumping", "shirt", "macaroni"]


accepted_words = []
# apply censor to words typed in
orig_words = originalsentence.split()
for word in orig_words:
   if word not in censoredwords:
       accepted_words.append(word)
accepted_sentence = ''
for word in accepted_words:
   accepted_sentence = accepted_sentence + word + ' '
print(accepted_sentence)
Output:
[size=small][font=Monaco, Consolas, Courier, monospace]Enter the text to censor: I had a papaya horny grasshopper toad jump,  jumping on my ugly shirt[/font][/size] [size=small][font=Monaco, Consolas, Courier, monospace]I had a horny toad jump, on my ugly[/font][/size]
Larz60+
Reply
#3
Thank you so much! I appreciate it a lot!
Reply
#4
how would I get the program to replace the words in the list with asterisks?
Reply
#5
   if word not in censoredwords:
      accepted_words.append(word)
   else:
      accepted_words.append('***')
Reply
#6
Thank you!
Reply
#7
If you prefer a single line, this would also do:
accepted_words.append('***' if word in censoredwords else word)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Case Insensitive Censor Function HNiChuimin 5 2,937 Jan-16-2021, 12:13 PM
Last Post: Serafim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020