Question

PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """...

PYTHON PLEASE Write a function to censor words from a sentence

code:

def censor_words(sentence, list_of_words):
"""
The function takes a sentence and a list of words as input.
The output is the sentence after censoring all the matching words in the sentence.
Censoring is done by replacing each character in the censored word with a * sign.
For example, the sentence "hello yes no", if we censor the word yes, will become
"hello *** no"
Note: do not censor the word if it is part of a larger word.
For example, if the sentence is "no hello no yes noooo no" and the censor word
is no, then the output should be "** hello ** yes noooo **".
"""
pass # write your solution here (do not change the rest of the program)
  

# The program asks the user to enter the list of words to censor and -1 to stop
censor_list = []
while True:
word = input("")
if word=="-1":
break
censor_list.append(word)

# Then, the program asks the user to enter the sentence to censor
sentence = input("")

# we call our function with user input and print the output
print(censor_words(sentence, censor_list))

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def censor_words(sentence, list_of_words):
    """
    The function takes a sentence and a list of words as input.
    The output is the sentence after censoring all the matching words in the sentence.
    Censoring is done by replacing each character in the censored word with a * sign.
    For example, the sentence "hello yes no", if we censor the word yes, will become
    "hello *** no"
    Note: do not censor the word if it is part of a larger word.
    For example, if the sentence is "no hello no yes noooo no" and the censor word
    is no, then the output should be "** hello ** yes noooo **".
    """
    for x in list_of_words:
        splits = sentence.split(" ")
        if x in splits:
            sentence = ""
            for y in splits:
                if y==x:
                    sentence += len(x)*"*"
                else:
                    sentence += y
                sentence += " "
            sentence = sentence[:-1]
    return sentence


# The program asks the user to enter the list of words to censor and -1 to stop
censor_list = []
while True:
    word = input("")
    if word=="-1":
        break
    censor_list.append(word)

# Then, the program asks the user to enter the sentence to censor
sentence = input("")

# we call our function with user input and print the output
print(censor_words(sentence, censor_list))

Add a comment
Know the answer?
Add Answer to:
PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CODE: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as...

    CODE: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as input. The output is the sentence after censoring all the matching words in the sentence. Censoring is done by replacing each character in the censored word with a * sign. For example, the sentence "hello yes no", if we censor the word yes, will become "hello *** no" Note: do not censor the word if it is part of a larger word. For example,...

  • In python using a def main() function, Write a function that will print a hello message,...

    In python using a def main() function, Write a function that will print a hello message, then ask a user to enter a number of inches (should be an integer), and then convert the value from inches to feet. This program should not accept a negative number of inches as an input. The result should be rounded to 2 decimal places(using the round function) and printed out on the screen in a format similar to the example run. Use a...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Write a program in python that asks the user to input a sentence. The program will...

    Write a program in python that asks the user to input a sentence. The program will ask the user what two letters are to be counted. You must use a “for” loop to go through the sentence & count how many times the chosen letter appears in the sentence. You are not allowed to use python built-in function "count()" or you'll get a Zero! Output will show the sentence, the letter, and the number of times the letter appears in...

  • Python Programming Q.3) Write a program that repeatedly asks the user to enter words until they...

    Python Programming Q.3) Write a program that repeatedly asks the user to enter words until they enter a period ("."). Your program should then print all of the words they entered including the period) on a single line separated by spaces. For example, Enter some words (. to stop): > hello > world hello world.

  • Write a Java program that reads in 5 words from the user and stores these words...

    Write a Java program that reads in 5 words from the user and stores these words in a String array. Then your program needs to print 2 lines of output with the following information: All words are in reverse order (based both on content and order). Every word at an even index of the array (i.e., indices 0, 2, and 4). For example, if user inputs "ABC", "DEF", "TY", "JK", and "WE". Then your program should output the following two...

  • this us python plz follow the instructions while u solving I need the code and sane...

    this us python plz follow the instructions while u solving I need the code and sane output QUESTION 1 Write a program that performs the following: 1) request a sentence (at least two words with no punctuation marks) input by the user 2) display the first word 3) display the last letter of the first word 4) display the last word 5) display the first letter of the last word input text can be any content just make sure to...

  • THIS CODE IS IN PYTHON #This program calls a function from the main function def getInput():...

    THIS CODE IS IN PYTHON #This program calls a function from the main function def getInput(): ''' This function gets the rate and the hours to calculate the pay ''' userIn = float(input("Enter the rate: ")) print(userIn) inputHours = float(input("Enter the hours: ")) print(inputHours) def main(): getInput() main() YOU NEED TWO FUNCTIONS : main() function get_inputs function

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • c++ Exercise #2: Words Count Write a program that prompts the user to enter a sentence,...

    c++ Exercise #2: Words Count Write a program that prompts the user to enter a sentence, then counts and prints the number of words in the sentence. Assume that there is more than one space between words of the sentence. Sample input/output: Enter a sentence: This is a 123test in There are 5 words in " This is a 123test \n"

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT