Question

In: Computer Science

For this assignment you will write a program with multiple functions that will generate and save...

For this assignment you will write a program with multiple functions that will generate and save between 100 and 10,000 (inclusive) "Shakespearian" insults. The program InsultsNetID.py is supplied as a starting point and it contains some tests that you must pass. The program loads words from three separate files: word1.txt, word2.txt and word3.txt. Each file contains 50 words or phrases. An insult is generated by choosing one word or phrase from each of the three lists of words at random, adding "Thou " to the beginning and "!" to the end. One such random insult would be:

    Thou artless bat-fowling barnacle!

With this many words, you could generate 50 * 50 * 50, or 125,000 possible unique insults - more than enough for anyone! Your program does not have to generate more than 10,000 unique insults and will not generate less than 100 of them for saving to a file. Your program will need to generate unique insults (no two the same) and they must be in alphabetical order before being saved in a text file. You should use Python's built in list searching and sorting functionality.

If your program is working properly it should generate an output like SampleOutput.txt. The actual insults will be different. And a file called "Insults.txt" containing all the insults will also have been created. The file will contain the user specified number of unique insults in alphabetical order. The user should be able to supply any number of insults between 100 and 10,000. Input of this number should be completely robust. If he enters something non-numeric or outside the legal range, he should be continuously re-prompted to enter a legal value.

Solutions

Expert Solution

# function to generate insults and

# store them into a file

def generate_insults():

   # open all files

   file1 = open("word1.txt")

   file2 = open("word2.txt")

   file3 = open("word3.txt")

   output = open("insults.txt", "w")

   # read phrases and words

   words1 = file1.readlines()

   words2 = file2.readlines()

   words3 = file3.readlines()

   # output list

   list = []

   for x in range (10000):

       # generate 3 random index

       i = random.randint(0,49)

       j = random.randint(0,49)

       k = random.randint(0,49)

       # generate a random insult

       insult = 'Thou '+words1[i].rstrip("\n")+' '+words2[j].rstrip("\n")+' '+words3[k].rstrip("\n")+'!\n'

      

       # don't add to list if it is duplicate

       if insult in list:

           continue

          

       # add insult to list and file

       list.append(insult)

       output.write(insult)

                   

# function to serve insults to a user

def serve_insults():

   n = 0

   # checks if user enter a integer value

   try:

       n = int(input('Enter number of insults: '))

   except:

       print('invalid input, try again')

       return serve_insults()

   # checks value in range

   if n < 100 or n > 10000:

       print('invalid input, try again')

       return serve_insults()

   # read all insults to a list

   file = open('insults.txt')

   insults = file.readlines()

   list = []

   # add n insults to list

   for x in range (n):

       list.append(insults[x])

   # return list

   return list

def main():

   generate_insults()

   print(serve_insults())

if __name__ == '__main__':

   import random

   main()


Related Solutions

*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
For this assignment, write a program that will generate three randomly sized sets of random numbers...
For this assignment, write a program that will generate three randomly sized sets of random numbers using DEV C++ To use the random number generator, first add a #include statement for the cstdlib library to the top of the program: #include <cstdlib> Next, initialize the random number generator. This is done by calling the srand function and passing in an integer value (known as a seed value). This should only be done ONE time and it MUST be done before...
Overview For this assignment, write a program that uses functions to simulate a game of Craps....
Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the player...
Assignment: (Save this file as A7-1.cpp) Write a program to calculate the gross pay for an...
Assignment: (Save this file as A7-1.cpp) Write a program to calculate the gross pay for an assembly line employee that works for a company that pays all assembly line workers $7.50 hour. Any employee that works over 40 hours per week is compensated by being paid time-and-one-half for each hour over 40. a. Use main( ) as the driver function. Allow the user to compute as many employees as desired. b. Write the function getName( ) that prompts for the...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
Write a program using multiple functions. Make use of an array to store data Make use...
Write a program using multiple functions. Make use of an array to store data Make use of searching techniques Read data from a file Write data to a file Instructions: In this lab, you will be examining a set of stock collected over a twenty four day period. Be sure to make use of an array to store these stocks. You will be required to read in the data points from a file. Write a function to read in the...
Write the Java program: In this assignment, you will create a program implementing the functionalities of...
Write the Java program: In this assignment, you will create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter). The Queue3503 class will contain: a. An int[] data filed named...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
In this assignment you will write a program that encrypts a text file.  You will use the...
In this assignment you will write a program that encrypts a text file.  You will use the following encryption scheme. Ask the user for the name of the original file. Ask the user for the name of the output file. Ask the user for the encryption key, n. Read n2 characters from the file into the n rows and n columns of a 2-dimensional array. Transpose the array.  (Exchange the rows and columns.) Write the characters from the array to an output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT