Custom Def #5: Dictionaries and Definitions

Published June 3, 2020, 1:37 a.m.

Last time we generalized our definition.  Now we will look at using python dictionaries in our process.  Dictionaries can help us organize our data and prepare for some long term storage of data.  Eventually we might like a madlib generator that saved our parts of speech from previous inputs and can recall and use them properly later.  But first we need to be comfortable using dictionaries.

Recall that dictionaries have a user defined index or 'keys'.    Lets start by making a dictionary with keys being the parts of speech we want and the value being an empty list.

MadLibDict = {
			  "noun": ['dog-catcher', 'plum', 'golf ball'],	
			  "adjective": ['ugly', 'fat', 'golden'],	
				}

Here our 'keys' are 'noun' and 'adjective' and our values are lists of those types of words.  To use these words we need to create another definition that selects a word from the list when requested.  Now we do not need to query for a word from the user or use the input function.  We simply want to extract a word from the list we have already created.  Our new definition needs to take as input the dictionary that we created and the part of speech we want and return the word that matches the part of speech. 

def getNewWordFromDict(WordDict, part_of_speech):
	# query = "Give me an %s, and press 'Enter'." % part_of_speech
	# engine.say(query, query)
	# engine.runAndWait()
	newword = random.choice(WordDict[part_of_speech])
	# newword = input()
	return newword

Notice that though, I copied the old definition, the only part of it that I kept was the call to return the 'newword'.  I used the random.choice function to randomly select an element from the lists.

Now we need to update our lines to use the new definition and be sure to supply both the dictionary and the part of speech that we want.

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..." % (getNewWordFromDict(MadLibDict,"adjective"), getNewWordFromDict(MadLibDict,"noun")))
# 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 arms on his own workout videos sponsored by P- 89 -X!" )
# lines.append( "The End!")

Now the program runs and randomly selects the stored adjectives and nouns without asking for user input at the command line.  In the end we want a code that would ask for user input and/or use stored inputs.  To do that we will have to store new input so that we can use it later.  We will do that next time using something called 'pickles'.

Thanks for watching.  See you in the next tutorial.

skip_nextCustom Def #6: Python Pickles
  • Custom Def #1: Import pyttsx3

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

  • Custom Def #3: Using Multiple Definitions

  • Custom Def #4: Generalized Definitions

  • Custom Def #5: Dictionaries and Definitions
    (currently viewing)
  • Custom Def #6: Python Pickles

  • Custom Def #7: Search and Replace in Lines

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