Custom Def #3: Using Multiple Definitions

Published May 30, 2020, 12:15 a.m.

Last time we were created a custom definition in Python and made a rudimentary madlib generator. This time we're going to see how easy it is to duplicate and change that definition to create multiple definitions.

First I want to show you another way to implement the definition that we wrote last time.  Recall that we defined 4 Nouns and then used them in the program:

Noun1 = getNoun()
Noun2 = getNoun()
Noun3 = getNoun()
Noun4 = getNoun()

Instead we can forget about declaring those 4 variables and get our nouns on-the-fly while we are appending lines to our list of lines:

lines = []
lines.append( "\n Now for the story! \n")
lines.append( "Once upon a time there was a %s with 2 weak arms." % getNoun())
lines.append( "The dog was very small and frail.")
lines.append( "One day a mean %s spoke to the dog and said..." % getNoun() )
lines.append( "'You are too weak.  You must eat protein %s!'" % getNoun())
lines.append( "The dog was annoyed, but he started excercising daily.")
lines.append( "Pretty soon, he became stronger and more muscular!")
lines.append( "Now the dog shows off his %s on his own workout videos sponsored by P-89-X!" % getNoun() )
lines.append( "The End!")

The simplest way of getting new parts of speech would be to simply copy our getNoun() definition and just change the name and the query:

def getAdj():
	query = "Give me an adjective, and press 'Enter'."
	engine.say(query, query)
	engine.runAndWait()
	newword = input()
	return newword

And this works!    Let's edit down our lines to just a single line and use our new definition for an adjective and and noun:

lines = []
# lines.append( "\n Now for the story! \n")
# lines.append( "Once upon a time there was a %s with 2 weak arms." % getNoun())
# lines.append( "The dog was very small and frail.")
lines.append( "One day a %s %s spoke to the dog and said..." % (getAdj(), getNoun() ))
# lines.append( "'You are too weak.  You must eat protein %s!'" % getNoun())
# lines.append( "The dog was annoyed, but he started excercising daily.")
# lines.append( "Pretty soon, he became stronger and more muscular!")
# lines.append( "Now the dog shows off his %s on his own workout videos sponsored by P-89-X!" % getNoun() )
# lines.append( "The End!")

executing the code:

C:\PythonProject>python madlib.py

The program calls for an adjective first and then a noun and renders them in the text right where we asked for them.

Note the added parentheses:

(getAdj(), getNoun())

after the '%' symbol.  Now that we are returning more than one variable into the text it is necessary to make all the objects to be returned into a tuple-- python objects contained within parentheses.

In theory, we could keep making modified copies of this definition to cover all of the parts of speech that we want to cover.  However, in the next tutorial I will show how we can generalize our definition to take some input to inform the output for any part of speech.  Thanks for watching!  See you in the next tutorial!

 

 

skip_nextCustom Def #4: Generalized Definitions
  • Custom Def #1: Import pyttsx3

  • Custom Def #2: Read a story from lines.

  • Custom Def #3: Using Multiple Definitions
    (currently viewing)
  • Custom Def #4: Generalized Definitions

  • Custom Def #5: Dictionaries and Definitions

  • Custom Def #6: Python Pickles

  • Custom Def #7: Search and Replace in Lines

  • Custom Def #8: Now for the Story...