Question

Create a Mad Libs program that reads in text files and lets the user add their...

Create a Mad Libs program that reads in text files and lets the user add their own text anywhere the word ADJECTIVE, NOUN, ADVERB, or VERB appears in the text file. For example, a text file may look like this:

The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was
unaffected by these events.

The program would find these occurrences and prompt the user to replace them.

Enter an adjective:
silly
Enter a noun:
chandelier
Enter a verb:
screamed
Enter a noun:
pickup truck

The following text file would then be created:

The silly panda walked to the chandelier and then screamed. A nearby pickup
truck was unaffected by these events.

The results should be printed to the screen and saved to a new text file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

#a method that takes a word, returns a word with all non alphabetical
#characters removed

def format(word):
    formatted=''
   
#appending only alphabets
   
for i in word:
        if i.isalpha():
            formatted+=i
    return formatted

if __name__ == '__main__':
    #getting input file name
   
input_file_name=input('Enter input file name: ')
    #opening input file in read mode
   
input_file=open(input_file_name)
    #reading text of input file
   
text=input_file.read()
    #closing input file.
   
input_file.close()
    #defininga variable to store the updated text
   
updated_text=''
   
#creating a list to store the adjectives
   
keywords=['ADJECTIVE','NOUN','ADVERB','VERB']
    #looping through each word in text (text is split by white space)
   
for word in text.split(' '):
        #assuming the word is not a keyword
       
is_keyword=False
       
#looping through each keyword
       
for temp in keywords:
            #checking if word after formatting equals current keyword
           
if format(word)==temp:
                #is a keyword
               
is_keyword=True
               
#getting characters after keyword, if exists (this can be a period,
                # a comma, question mark or anything)
               
suffix=word[len(temp)::]
                #if the keyword is adjective or verb, using 'an' for prompt, else using 'a'
               
a_or_an = 'a'
               
if temp == 'ADJECTIVE' or temp == 'ADVERB':
                   a_or_an = 'an'
               
#asking for replacement word
               
replacement = input('Enter ' + a_or_an + ' ' + temp.lower() + ': ')
                #appending replacement word along with original suffix (period, comma etc) and a
                #whitespace
               
updated_text += replacement +suffix+ ' '
               
break #exiting inner loop
        #after the loop, if the word is not keyword
       
if not is_keyword:
            #simply appending the word, and a white space
            
updated_text += word + ' '
   
#removing trailing/leading blanks from text
   
updated_text=updated_text.strip()
    #asking for output file name, opening in write mode
   
output_file_name=input('Enter output file name: ')
    output_file=open(output_file_name,'w')
    #writing updated_text, saving and closing
   
output_file.write(updated_text)
    output_file.close()

#output

Enter input file name: template.txt

Enter an adjective: silly

Enter a noun: chandelier

Enter a verb: screamed

Enter a noun: pickup truck

Enter output file name: output.txt

#template.txt

The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events.

#output.txt

The silly panda walked to the chandelier and then screamed. A nearby pickup truck was unaffected by these events.
Add a comment
Know the answer?
Add Answer to:
Create a Mad Libs program that reads in text files and lets the user add their...
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
  • You will create your own silly story based on information provided by the user. For example,...

    You will create your own silly story based on information provided by the user. For example, the parts in bold were entered by the user. For ideas see: Mad Libs. My Silly Story name: Frank Zhang whole number: 345 body part: stomach noun: cup floating point number: 1.23 clothing article: hat destination: Colorado goal: degree The resulting story is: Congratulations! Today is your 345 day. You're off to Colorado! You're off and away! You have brains in your stomach, You...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

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