< Prev  |  Home  |  Next >

Python Dictionaries

Dictionaries are… actually you probably know what they are already. You just look up a word to find out its meaning. In Python, they are a bit more general than that; you can use any ‘word’ (the key) to look up something about it.

For example, here’s a dictionary to store users’ passwords:

There are two users, Amina and Martin, both with super-lame passwords. If I wanted to get a user’s stored password, I’d just use the key (in this case the key is their name):

A dictionary can be used to link any two pieces of data: names and passwords; names and email addresses; ingredients and quantities for a recipe, or even character substitutions for translating words into Leet, like in this next example.

In leetspeak, the word ‘hello’ would be written as ‘h3110′, converting some characters into a similar looking number or set of characters. Let’s store these translations in a dictionary called ‘leet’:

If we want to look up a leet conversion for a character, we just type:

Which gives:

3

The program above would be great for translating one character at a time, but how about translating whole sentences? Here’s how the program could work:


set up the dictionary
print a welcome message
create a variable called 'textOut' which is the translated text (initially empty)
ask the user for the text to translate, and store in a variable called 'textIn'

for each character in 'textIn'
   if the character is in the leet dictionary
      convert the character and add it to the output text string
   else (there is no translation to make)
      just add the original character to the output text string

This is a relatively simple program, and here’s how it might look in Python:

And here’s the program in use:

l337 (0nv3r73r
==============
Type in a sentence ('q' to quit)
>hello world
h3110 \/\/0r1[)
Type in a sentence ('q' to quit)
>notice how this program only changes some of the characters
n07i(3 h0\/\/ 7hi5 pr06r4m 0n1y (h4n635 50m3 0f 7h3 (h4r4(73r5
Type in a sentence ('q' to quit)
>and leaves the rest alone
4n[) 134v35 7h3 r357 410n3
Type in a sentence ('q' to quit)
>q
q

Notice how the program always prints the translation, even when we just type "q" to quit at the end. Would you be able to alter the program to stop this happening?

You could also improve the program by allowing the user to add a character to the leet dictionary (if it's not already there):

And to remove items from the dictionary (but only if they are in it):

Printing the dictionary is easy, you just type:

Challenge

Create a program to translate words from one language into another. It can be from English into a second language you may speak, or it could even be an English to text-speak translator for people over 30. (Sorry if you are over 30, I didn't mean to cause offence. IWKKD! :P.) You could also include a menu, and allow the user to see the dictionary, and to add and remove words.

For this challenge, you want to take the input and translate it one word at a time, rather that one character at a time. So:

Won't work. If you try typing:

Then you'll see that it prints each character of the sentence separately. Instead we have to split up the text into words by doing this:

Try it. The split() bit takes the text and splits it up whenever it sees a space.

< Prev  |  Home  |  Next >