Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. (35 Points) Write a function madlibs() that takes in 4 parameters. A list of

ID: 3699350 • Letter: 3

Question

3. (35 Points) Write a function madlibs() that takes in 4 parameters. A list of nouns, a list of verbs a list of adjectives and some text. The text will have NNN for a place to inject a random noun from the list, VVV to inject a random verb from the list and AAA to inject a random adjective from the list. The function will print the text with the values replaced. Here is some sample output. HINT: Break the text up into individual strings. Here are three sample runs of the lists and sentences found in the template. Feel free to edit the lists and make up your own text templates.
The hungry girl ran at the father A guard looked at the dog and then shouted at the hungry guard --RESTART: ?:/users/azoko/ Desktop/ The hungry donkey sang at the teacher A teacher looked at the dog and then sang at the stunned father RESTART : C:/Users/azoko/ Desktop/. == The stunned dog flew at the boy A policeman looked at the donkey and then shouted at the silly dog

Explanation / Answer

import random

def madlibs(noun, verb, adj, sen):
sentence = sen.split(" ")
for i in range(len(sentence)):
if sentence[i] == "AAA":
sentence[i] = adj[random.randint(0, len(adj)-1)]
elif sentence[i] == "NNN":
sentence[i] = noun[random.randint(0, len(noun)-1)]
elif sentence[i] == "VVV":
sentence[i] = noun[random.randint(0, len(verb)-1)]
print " ".join(sentence)

Sample code to test the code
noun = ["cat", "dog"]
verb = ["walking", "sleeping"]
adj = ["white", "black"]
sentence = "The AAA NNN VVV on the NNN"
madlibs(noun, verb, adj, sentence)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote