CODE IN PYTHON The story generator the Madlibs game asks fo

CODE IN PYTHON

The story generator – the Mad-libs game asks for parts of speech, such a noun, adjective, or adverb, and those words are plugged into a template to generate a sometimes-funny story.

For this problem, write a function that plays a game of Mad-libs. Your function (MadLib) will be given an integer parameter to select the story from a list of story templates and will return the MadLib string with words from the user substituted for the placeholders. You program will store the templates as strings and ask the user for appropriate parts of speech to fill in the template based on the given parameter.

Your program needs to include the following templates.

1. “Be kind to your -footed , or a duck may be somebody’s .”

2. “It was the of , it was the of .”

3. ? I don’t have to show you any !

4. My always said was like a box of . You never know what you’re gonna get.

5. One , I a in my pajamas. How he got in my pajamas, I don’t know.

If for example, the function is called as MadLib(2), your function should ask for entries for each of the placeholder words in the template, such as:

“Enter adjective1:”

“Enter noun1:”

“Enter adjective2:”

“Enter noun2:”

Each template will require multiple words of differing types to be inserted into the string. Substitute the user words for the placeholder and return the completed string.

Solution

Code:

import random
def MadLib(n):
   t = [\"Be kind to your -footed , or a duck may be somebody’s.\",
   \"It was the of , it was the of .\",
   \"I don’t have to show you any !\",
   \"My always said was like a box of . You never know what you’re gonna get.\",
   \"One , I a in my pajamas. How he got in my pajamas, I don’t know.\"]
  
   inp = []
   for i in range(n):
       str1 = \"Enter the adjective \"+str(i+1)+\":\"
       str2 = \"Enter the noun \"+str(i+1)+\":\"
       inp.append(input(str1))
       inp.append(input(str2))
      
   out = []  
   for i in t:
       t1 = i.split(\" \")
       for i in range(len(inp)):
           r1 = random.randint(0,len(t1))
           t1[r1] = inp[i]
       out.append(\" \".join(t1))
   return out

out = MadLib(2)
for i in out:
   print(i)

Output:

CODE IN PYTHON The story generator – the Mad-libs game asks for parts of speech, such a noun, adjective, or adverb, and those words are plugged into a template
CODE IN PYTHON The story generator – the Mad-libs game asks for parts of speech, such a noun, adjective, or adverb, and those words are plugged into a template

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site