How to Create the Dictionary Look-Up Function in Python on the Raspberry Pi

By Sean McManus, Mike Cook

To move beyond basic functions in Python, create the dictionary look up function. The Raspberry Pi allows you to build a function that takes the player’s text and checks for any relevant responses. To do this, you’ll use dictionaries and functions, and add in some new ideas relating to loops, strings, and decision-making.

The function is only 12 lines long, but it’s quite sophisticated. It needs to take what the player entered, and check each word in it to see whether the dictionary has a response for that word. The player might use more than one word that’s in the dictionary.

For example, if the player says “I love pop music,” the words love and music might both be in the dictionary. Alternatively, the player might use no words the program recognizes, so you need to design your function to cope with that situation too.

Here’s the function in its entirety, so you can see how all the bits fit together:

def dictionarycheck(message):
 for eachword in playerwords:
  if eachword in chatdictry:
 if smartreplies:
  return smartreplies[replychosen]
  return "

The next two lines introduce string methods. These are like built-in functions that are attached to a string and transform it in some way. The lower() method converts a string into lowercase. This is important because if a player uses capital letters or mixed case, they won’t match the lowercase words you’ve used in your dictionary keys.

As far as the program is concerned, hello and Hello aren’t the same thing. The split() method takes a string and splits it into a list of its constituent words. The first two lines in our function, then, turn the contents of the message variable into a lowercase version of itself, and then create a new list of the words the player entered, called playerwords.

You’re going to store possible replies to the player in a list called smartreplies, so create that as an empty list.

The next step is to set up a loop that goes through the list of words that the player entered in turn. When you used a for loop, you’re going to work your way through a list of words. Each time around the loop, the variable eachword contains the next item from the list of words the player entered.

The next line introduces a new idea, the conditional statement, which starts with if. A conditional statement is used to enable the computer to make a decision about whether it should carry out certain instructions. You’ll come across it in almost every program you write. Here, it’s being used to avoid the program stopping and reporting an error if you try to use a key that isn’t in the dictionary:

  if eachword in chatdictry:

The eachword variable contains one of the words the player entered, so the if statement checks whether that word is in the dictionary and only carries out the next two instructions if they are.

Notice how indenting is used here to show which commands belong together — in this case, which commands are controlled by the if statement. If the word is in the dictionary, the program looks it up and adds the resulting response to the smartreplies list, using append().

This process is repeated for every word the player entered, but that’s all that happens in the loop. The next line is not indented below the for statement, so it’s not controlled by it.

When you come out of the loop, check whether the list smartreplies has anything in it, by using simply

 if smartreplies:

In English, this means “if smartreplies has content in it.” The commands indented underneath that are carried out only if some entries were added to the smartreplies list, which only happens if one or more of the words the player entered were found in the dictionary.

In that event, you want to return one of the items in the smartreplies list to the main program, so pick one at random from the list and use return to send it back to the main program and exit the function.

After that, use the else command. In plain English, this means otherwise, and it’s joined to the if command. So if smartreplies has content in it, the commands are carried out to send back an appropriate reply, chosen at random.

When none of the player’s words were found in the dictionary and so smartreplies is empty, the instructions indented underneath the else command are carried out instead. The function sends an empty message (“”) back to the main program and exits the function.