The Python Pals Program A Problem
As mentioned earlier, I've been inspired by the Byte Brothers to create a mystery story which kids have to solve using their l33t h4x0r skillz in Python.
This is loosely based on The Byte Brothers Go to a Getaway, by Lois and Floyd McCoy. My well-thumbed copy was printed in 1984. I'm not sure when I got it, but I remember being around 8 or 9 when I first started writing programs in BASIC. Sadly, BASIC is as outdated as the Lascaux Cave Paintings, so I've written this in more friendly Python.
My hope is that people will write a bunch of interesting mystery stories (using these characters or their own) release them as Creative Commons, then we can put out a book AND SAVE THE WORLD!
Ahem...
Without further ado, I present to you the first adventure of "The Python Pals"!
This work is licensed under a Creative Commons Attribution 2.0 UK: England & Wales License.
The Morse Code Mystery
The Beachcombers
The waves crashed angrily on the beach, and the rain seemed to come in from all directions. A sudden gust blew a thin drizzle straight down Poppy's collar. "This weather is foul," she spat. "Just five more minutes," yelled Penny.
Poppy collapsed herself until she was eye level with her friend Penny. Even though Penny was the shorter of the two, she was hunched down on the beach anxiously glancing at the shoreline.
"Penny, what are we doing here? It's cold, wet, and most importantly," she paused for dramatic effect, "We're going to miss the Food Network's 'Marzipan Marathon'!" Her friend giggled - neither of them could stand those TV cookery channels - but they had agreed there was something oddly compelling about watching a man trying to eat his body weight in cake. "There!" Yelled Penny, pointing a little way down the shoreline. "I told you that interesting stuff gets washed up here when there's a storm. Grab it!"
Penny was used to being bossed around by Poppy. Despite being the older of the two friends, she was always being treated like Poppy's younger sister - so she relished in the chance to bark orders at her.
Penny scooped up the large square of blue canvas a spun around. "Please can we go in now?" she whined, "It's cold and wet and..."
Before she could even finish her sentence, Poppy exclaimed "Look! Look at the cloth!"
They both stared at the raggedy square of canvas. It was peppered with strange white marks.
"Let's get home and work out what this is," said Poppy. With that, she snatched the cloth from Penny, and started sprinting away toward her parent's home.
"That just what I was about to say!" yelled Penny, as she slowly trudged up the beach.
Pi Power
"Will you be wanting some soup, girls?" asked Poppy's mum, Ada. She had gotten used to both of them storming in and expecting to be fed. "No time Ada, thanks," said Penny breathlessly, "We've got to work out what this is."
"Hmmm, looks like old fashioned Morse Code to me," said Ada after a moment's cursory examination of the cloth. "Where on Earth did you find..."
"Can we get some soup and croutons, and maybe some cheese. Do we have any crisps?" yelled Poppy as the girls bolted upstairs.
They flipped on the Raspberry Pi. It had been a birthday present from Poppy's dad. One of the few things she had left to remember him by. Just as Linux was quickly booting up, there was an enormous crack of thunder which shook the whole house. The lights flickered, then went off.
A second later they were back on, but there was a strange burning smell.
"Oh no! Cried Penny, "The router!"
Where their Internet router had been was a smouldering lump of twisted plastic and metal.
Poppy said a word which, if her mother had overheard her would have meant an instant reduction in pocket money. "How will we find out what 'Morse Code' is if we can't get online?"
The girls sat, stunned. No Internet. No Facebook. More importantly, no Wikipedia!
They looked at their phones but, as always they didn't have any credit.
Poppy looked around the room forlornly. The little study had been Poppy's dad's work area before he... Well. Just before. As she scanned the shelves looking for something which might help - even an old fashioned dial-up modem would do.
Penny suddenly chirruped, "Pops, what's the 'Encyclopedia Britannica'?" Pointing to an imposing set of books on a shelf. "No idea," sniffed Poppy despondently. "Only, and I know it was your dad's, but 'encyclopedia' sounds a bit like 'Wikipedia' and I wondered..." "Of course! Quick, which one of the books has 'M' printed on the side?" Poppy smiled. It was rare to see her smile these days, and Penny relished cheering her friend up.
They plucked the dusty volume from the shelf and opened it up more-or-less at random. Its musty smell filled the room and they began flicking through the yellowing pages. "Where's the search function on this thing?" joked Penny. "Möbius, Moomins, Mormon, Morse!"
There, laid out in the pages of the encyclopedia was a guide to Morse Code.
The girls flipped on the Pi and waited for it to boot.
"The most important thing to consider when programming," said Poppy doing her very best imitation of Mr Cartwright their inexcusably dull IT teacher. "...is planning!" said Penny, finishing her friend's sentence.
"We want a program which will let us type in each letter of Morse Code and then show us the English translation."
Penny grabbed the keyboard. She knew that she would be doing most of the typing while Poppy did most of the pointing out of her mistakes. Apparently, this was known as "pair programming" according to Mr Cartwright. She didn't think much of it.
"First," said Poppy, "Let's define a dictionary of English and Morse Code."
Penny's hands darted across the keyboard as her friend read the dots and dashes from the book.
morse = { '.-' :'A', '-...':'B', '-.-.':'C', '-..' :'D', '.' :'E',
'..-.':'F', '--.' :'G', '....':'H', '..' :'I', '.---':'J'
}
You can program the rest of the dictionary yourself.
Poppy sighed, "Why on Earth have you bothered to line up all the letters?" "It helps me think," said Penny defensively, "I like it neat. Besides, Python doesn't care how I space things like that." "Fine! Let's do a quick test to see if it works," said Poppy. "Right, I'll ask the user to type a single letter of Morse Code. Then it's really easy to look it up in the dictionary," said Penny.
get_code = raw_input('Type a Morse Character ')
print morse[get_code]
She ran the program. As expected it asked her to type in a code. She hit the sequence ".-" and hit enter.
She was rewarded with a glowing "A" on the screen.
"Good start," said Poppy. "But it's going to be a pain to type in every Morse character, then hit enter, then try to string the letters together."
Penny thought for a moment. She knew she could come up with something clever.
"How about we read in the string we type in, then break it up based on where the spaces are?" She said tentatively. Poppy looked confused, "What do you mean, Pen?"
Penny ran Python directly so she could show what she was thinking
Python 2.7.3 (default, Aug 1 2012, 05:14:39) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
At the prompt she typed
>>> "This is a test".split()
"If I'm right, the .split() function will give us an list of all the word."
She gently thumbed the enter key.
['This', 'is', 'a', 'test']
"Yes!" Shouted both girls simultaneously.
"So now we will have each Morse Letter in a list, then we want to look it up in the dictionary." "That's right. Luckily it's super simple to do something on every item in a list - look,"
Penny typed directly into Python again.
>>> list_of_words = "This is a test".split()
>>> for item in list_of_words:
... print item
But this time, when she hit enter, an error message popped up!
File "", line 2 print item ^ IndentationError: expected an indented block
This time it was Penny's turn to swear.
"Wipe that grin off your face! Yes, spacing and indentation are important in Python. But generally it's fine if I line up my code just how I like it."
She tried again. This time hitting the spacebar as hard as she thought it could stand.
>>> list_of_words = "This is a test".split()
>>> for item in list_of_words:
... print item
This time, the result was a lot more satisfactory.
... This is a test >>>
"Getting there," muttered Poppy under her breath. They swiftly changed the last few lines of their code to read:
get_code = raw_input('Type in the Morse Code ')
code_list = get_code.split()
for item in code_list:
print morse[item]
"Ok," said Penny, "Let's see what this mysterious blue cloth says on it..."
... --- ... -... --- .- - ... .- -. -.- --- -. .. ... .-.. .- -. -..
They checked and double checked what they had typed in.
"Ready?" said Poppy. "Let's do it!" said Penny as she hit the enter key.
Saving The Day
"Well done you two," said the Mayor. "Without your help, that sailor would have been stuck there a long time."
Penny and Poppy both grinned. It had all been such a rush, they'd gone from running their code, to screaming for Poppy's mum, to calling the coastguard, to seeing a shipwrecked sailor being rescued. They had been interviewed for TV and the clip had hundreds of YouTube likes.
There was even a Facebook page dedicated to thanking them!
The Mayor had decided that such fine upstanding citizens deserved a reward - and had invited them to a special event in the town hall.
"Who would have thought two girls would know Morse Code?" They Mayor said, "Did you learn that at school?"
Penny fixed him with a steely glare, "No. We taught ourselves."
The Mayor peered over his glasses, "But surely you had help from someone? An older brother perhaps?"
Poppy knew the look on Penny's face and answered before her friend lost her temper, "I think you'll find," she said sweetly, "that women don't need help from boys!"
Poppy felt Penny's hand slip in to hers. She started to chuckle as her hand was squeezed in firm but measured bursts.
- .... .- - ... .-- .... -.-- -.-- --- ..- .- .-. . -- -.-- -... . ... - ..-. .-. .. . -. -..
THE END
What Next
- Write your own story - using these character or ones you've created.
- Make the Python simple and readable - explain as you go.
- If there's enough interest, I'll gather them all up, maybe on a central site.
- Perhaps, if people like them, we can release a (free) ebook?
- Oh, and feel free to criticise my storytelling abilities or coding deficiencies!
matt says:
An excellent endeavor!
I (fondly) remember the Micro Adventure books...
http://en.wikipedia.org/wiki/Micro_Adventure
Keep it up! Things are more complex now, but kids still need cool introductions to computer programming.
Terence Eden says:
Oooh! I hadn't heard of those before. I'm ordering a complete set of Byte Brothers to see what other coding problems are simple enough.
PaulS says:
Interesting and distracting. I applaud your initiative. A couple of observations (not criticisms I hasten to add).
I found it difficult to get clear in my head which girl was which (probably due to lack of sleep more than anything else). No doubt this will sort itself out as the series continues. My son was slightly uncomfortable with the protagonists being all female and had to be persuaded that it was a mystery/game for everyone. Maybe a third male character at some point?
I liked the hint of mystery regarding the Father. (Will there be a prequel :-)?)
The first of set of Morse was fairly well spaced but the second was smaller and this led to transcription errors (exaggerated spacing would probably help for that).
Part way through my son asked me "Why didn't they just write on the canvas rather than use code" I was unable to provide an explanation that satisfied him.
What do you think about adding a bit of hardware hacking in future episodes? It seems to be very popular on the RPi site.
"When's the next one?" (Final comment from my son)
Terence Eden says:
Hey Paul and Son,
Even I get confused between Penny and Poppy. Perhaps Thompson and Thomson for the next one 🙂
It would probably be a good idea to make the characters male and female - perhaps brother and sister? I just wanted to make a counterpoint to the originals (which were brothers) in order to get young women interested.
Prequels, sequels, and spinoffs 🙂 And feel free to write your own.
Good point on the spacing, I'll see if I can adjust the blog to accommodate it.
There wasn't enough space on the canvas, sadly. How old is your son - and when does he enter the Police Force?
Hardware hacking is a little outside of my area of expertise. I also wanted to create something which wouldn't need anything more than a Pi / Computer. Once you tell people they need to buy diodes and soldering irons, it can get a bit complex.
I've just finished writing the next one. It will be published early next week.
Thanks for the feedback - I really appreciate it.
mags says:
PaulS: I think it's a very good thing for your son to be exposed to good stories with an all female cast.
Lily Dart (@lily_dart) says:
Absolutely legendary that the protagonists are female. And the wikipedia comment is very funny.
Craig H says:
"Oh no! Cried Penny, "The router!" chortle
Minor nit pick: "relished in" -> "revelled in"?
Terence Eden says:
Well, I had to find some way to prevent them just Googling "Morse Code" 🙂 I think relished can be used there, but I'll defer to my editor.