In: Computer Science
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.
# 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()

