I am working my way through Zed Shaw‘s “Learn Python the Hard Way“, which is both a book with a DVD containing videos of each of the lessons and a web site that does not have the videos. So far, I am really enjoying it. I’m on lesson 36 and I’m only about half-way through the book. One of the things that I really like about it is that he doesn’t try to cram ALL THE THINGS into a single lesson. It is all really bite-sized. In fact, he frequently lists “Search the Web for additional info and features” as part of the Study Drills. As a result, I am getting very familiar with the official Python docs site. Also, the pace is not hurried. There have been several lessons where he says “Work on this for a week”. One of these type lessons was on Boolean Logic. He said make flash cards on, you know, index cards. I used Tiny Cards, which is a new app from the Duolingo people, to create my deck. This seems to work really well.
The reason for this post is not to shill for Zed, but rather to brag about myself, of course. Lesson 36 is to create a Zork-like, text-based adventure game. The lesson is in nested if-else statements and user defined functions. Zed stopped just short of saying “Go hog-wild with it! Throw everything you can think of at this, including the kitchen sink.” Sure, I could have gone deeper, but I am still pretty pleased with what I came up with.
Step 1 in the lesson was to draw out a map and then start to figure out what the different decision points should be. I am going to be honest with you right now: I’m not terribly imaginative. I am sure that plenty of middle-aged dad’s feel the same way. But, what I have as a resource is a five year-old son with a very active imagination, a passion for drawing detailed, full-page scenes, and a love of helping his dad in any way possible. If you could have seen his little face when I asked him to draw up a map to help me out with this lesson! Well, he did a great job.
I asked him about it when I got home from work last night. He already had it out and waiting for me at the dinner table for the walk-through. I made notes on when he told me about the adventure. After he went to bed, I set to work on turning this creation of his imagination into a playable reality. Of course, I knew he was going to be disappointed that it was text-based and not a game with graphics, so I kept that in mind in my writing. It had to be entertaining, and I borrowed some phrasing from his favorite chapter book series, “The Magic Treehouse“. I told him this morning that it was ready for him to play, but he would need his mother’s help. They were soon laughing in the right places and giving pointers on things that could be improved. The biggest critic, of course, was the boy. Apparently, a trap door is supposed to open automatically and I missed that. 🙄 One of the features that I added was a pseudo-randomizer and on every turn there is a 25% chance that you get eaten by the grizzly bear.
import random
...
result = random.randint(1, 4)
if result == 4:
bearMeal()
...
def bearMeal():
print """
Too late!
The bear got you before you could escape!
You are inside a grizzly bear stomach.
It is dark and smelly, but you don't mind...
because you are dead.
The end.
"""
exit(0)
The result was a fun game that both of the players enjoyed. It only took a few hours and I figured out a cool way to use a uniformed prompting function to get input from the player.
def prompt():
print
choice = raw_input("What do you do? ")
return choice
...
choice = prompt()
...
That is going to come in handy in future Python scripts.
So, props to Zed for this course. It is far more detailed than I figured it would be when I started. It is teaching me far better coding practices than the Cisco DevNet programming course, which is also based on Python. It is going to be a great asset to have these lessons under my belt as the Kyle Byers PyNet course ramps up.