Question

Program Instructions Write a program called censor.py. This program will have the following functionality and requirements:...

Program Instructions

Write a program called censor.py. This program will have the following functionality and requirements:

  • Has a main function:
    • That asks the user to input a sentence
    • Then asks the user to enter a list of words they want to replace (separated by spaces)
    • Calls a function called replaceMultiWords passing those two parameters, which returns a filtered string
    • Prints out the filtered (censored) string to the console
  • Has a replaceMultiWords function that:
    • Takes a string "sentence" and a string "words" in as parameters. (Note: "words" is a string containing the words to replace, separated by spaces)
    • Does appropriate splitting on the "words" string and in a loop calls the replaceWord function to replace it in the string
    • Returns a sentence with all occurrences of all words replaced
  • Has a replaceWord function that:
    • Takes a string "sentence" and string "word" (a single word to replace)
    • Replaces the "word" in "sentence" with dashes (equaling the number of letters in the word)
    • Return the sentence with the single word replaced throughout
  • Has a conditional call to main
if __name__ == "__main__":
    main()

Skeleton of needed code:

def main():
    # Get input for the phrase/sentence
    # Get input for multiple words to replace
    print(replaceMultiWords(s, w))

def replaceMultiWords(sentence, words):
    # Split "words" by space
    # Loop through each words, and call replaceWord(s, w)
    # return sentence when done looping

def replaceWord(sentence, word):
    # Replace word in sentence with equal amount of dashes
    # return sentence

if __name__ == "__main__":
    main()

Homework Answers

Answer #1

Program In Python :-

def main():
    # Get input for the phrase/sentence
    s = input("Input a sentence : ")
    # Get input for multiple words to replace
    w = input("Enter a list of words you want to replace (separated by spaces) : ")
    print(replaceMultiWords(s, w))

def replaceMultiWords(sentence, words):
    # Split "words" by space
    lst = words.split(" ")
    # Loop through each words, and call replaceWord(s, w)
    for i in lst:
        sentence = replaceWord(sentence,i)
    # return sentence when done looping
    return sentence

def replaceWord(sentence, word):
    # Replace word in sentence with equal amount of dashes
    l = len(word)   #find the length of the word
    dashes = '-' * l
    sentence = sentence.replace(word,dashes)
    # return sentence
    return sentence

if __name__ == "__main__":
    main()

Output:-

Know the answer?
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
Similar Questions
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Lab Assignment | Count vowels Write a program that will use a while loop to count...
Lab Assignment | Count vowels Write a program that will use a while loop to count and display the number of vowels in the string provided as input by the end-user. This program will consider the letters a, e, i, o, and u (both upper and lower case) to be vowels. The user may enter a single word, a sentence, a paragraph, or nothing at all as input. Be sure to test each possibility. Python 3 please!
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
PYTHON: Write a function called keepShouting. This function does not need any input parameters. The function...
PYTHON: Write a function called keepShouting. This function does not need any input parameters. The function should simply repeat whatever the user enters but in all capitals! The function should ask the user to enter a phrase. The phrase then should be capitalized and printed to the screen. If the user just hits return (which will produce the empty string) then the program should stop. Hint: Use a while loop and string methods # keepShouting() """ The function call should...
Consider the following python script mt_q2.py, which defines the function called now(): #!/usr/bin/env python3 # program:...
Consider the following python script mt_q2.py, which defines the function called now(): #!/usr/bin/env python3 # program: mt_q2.py import os def now():     p = os.popen('date +%Y%m%d')     today = p.read().strip()     return today if __name__ == '__main__':     print(now()) Asnwer the following questions: (a) What type of object will be returned by the now() function? (b) If you run the above Python script "python3 mt_q2.py" on a system with real-time clock on Oct 16, 2019 at 10:00 am, what will...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following:...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following: 1. The main program calls the function enterNum 3 times. The first time enterNum is called, the main program stores the returned output in the variable xx. The second time, the returned output is stored in the variable yy and the third time in zz. 2. enterNum asks the user to enter a floating point number. This function has no input arguments and returns...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a program in python reverse each and every word of the sentence using function taking...
Write a program in python reverse each and every word of the sentence using function taking string as input parameter from user and passing that string into function and also explain the logic ======================================= input: 'Exam is gonna be good' output: 'maxE si annog eb doog' ================ i am trying like this but getting confused '.'join([w::-1] for w in s.split()]) i need more focus on explanations because it is making me confuse i am not clear with concepts so please...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT