List Censor Thread - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: List Censor Thread (/thread-295.html) |
List Censor Thread - palmtrees - Oct-05-2016 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=0Any help is appreciated. Thank you RE: List Censor Thread - Larz60+ - Oct-05-2016 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) Larz60+
RE: List Censor Thread - palmtrees - Oct-05-2016 Thank you so much! I appreciate it a lot! RE: List Censor Thread - palmtrees - Oct-05-2016 how would I get the program to replace the words in the list with asterisks? RE: List Censor Thread - nilamo - Oct-05-2016 if word not in censoredwords: accepted_words.append(word) else: accepted_words.append('***') RE: List Censor Thread - palmtrees - Oct-05-2016 Thank you! RE: List Censor Thread - nilamo - Oct-05-2016 If you prefer a single line, this would also do: accepted_words.append('***' if word in censoredwords else word) |