Question: How can I make it so this code doesn't have an error in the output and generates random shakspeare phrases for each different file. import
How can I make it so this code doesn't have an error in the output and generates random shakspeare phrases for each different file.
import random
def loadphrasesfilename:
Reads data from a file and returns a list of phrases.
Each line in the file represents one phrase.
:param filename: The name of the file to read
:return: A list of phrases from the file
try:
with openfilenamer as file:
return linestrip for line in file
except FileNotFoundError:
printfError: The file filename does not exist."
return
def selectrandomphraselist:
Selects a random phrase from a list.
:param phraselist: List of phrases
:return: A randomly selected phrase
return random.choicephraselist if phraselist else "UNKNOWN"
def combineinsultphrase adj adj noun:
Combines the given phrases into a Shakespeareanstyle insult.
:param phrase: The introductory phrase
:param adj: The first adjective
:param adj: The second adjective
:param noun: The noun
:return: A formatted insult string
return fphrase Thou adjadjnoun
def main:
Controls the program by generating insults in a loop until the user decides to stop.
# Load phrases from files
phrases loadphrasesphrasestxt
adjectives loadphrasesadjtxt
adjectives loadphrasesadjtxt
nouns loadphrasesnounstxt
if not phrases and adjectives and adjectives and nouns:
printError: One or more files could not be loaded. Exiting program."
return
printWelcome to the Shakespearean Insult Generator!"
printGenerate insults to your heart's content, or type 'stop' to exit."
while True:
# Generate a random insult
phrase selectrandomphrases
adj selectrandomadjectives
adj selectrandomadjectives
noun selectrandomnouns
insult combineinsultphrase adj adj noun
# Display the insult
print
insult
# Ask if the user wants another insult
userinput input
Would you like another insult? yesstop: striplower
if userinput 'stop':
printGoodbye thou art truly magnificent!"
break
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
