Making Class Memorable with Basketball-Inspired Python Example

A little program to calculate game logs and stimulate student interest.

“I cannot be a teacher without exposing who I am.”

—Paulo Freire


For the last twenty-something years with rare exception, my five best friends from Carnegie Mellon and I have upheld an annual tradition: we get together in a different city, catch up, eat well, and play three-on-three-basketball. The photo above comes from this year’s match in Colorado. (Note that the backboard’s no-dunking sign couldn’t be less relevant to these six individuals, but I digress.)

It’s an understatement to say that we take the hoops part of our weekends pretty seriously. If the uniforms don’t tip you off, let me give you a little more information. There’s the frequent smack talk that takes place during the planning stages. Of course, we film the series. Beyond that, we rent a gym to ensure that the weather isn’t a factor. For the past three years, we’ve hired a proper referee because we can’t be remotely objective in the heat of battle. As for our team name, it’s the same moniker as the one that we gave ourselves during our CMU years: Reservoir Dogs, a nod to Tarantino’s masterpiece.

Why not demonstrate an important programming concept in a memorable way?

My favorite part of the weekend is watching the game footage as my friend Chris tallies the stats. Over the years, we’ve had to create a few new categories because the six of us find traditional basketball measures lacking. For instance, no NBA stat accurately captures really bad shots. This led to the invention of uglies, a category that includes airballs, bricks, and other ill-advised shots.

Shooting a decent percentage isn’t really an option for me, especially on a consistent basis. I’m downright thrilled to flirt with 30 percent in any given game. My assist numbers usually aren’t too shabby, but I’ll credit my teammates more than myself although occasionally I’ll channel my inner Jason Kidd.

Finding a Teaching Hook

I was thinking about this last week as I was demonstrating Python’s for loops to my students and now to nest them within each other. I had already shown them simple while loops a little “while” ago. (Not a bad pun, eh?) To this end, I wrote the following program to calculate per-game and total shooting percentages. Give it a try yourself if you like below.

The nested loops above (i and j) demonstrate how a programming language cycles through a previously unknown number of events (games and shots, respectively). It would not take that much effort for a curious student to tweak my code above for any number of different scenarios. Case in point: A couple of changes could turn the program above into a rudimentary weekly sales-conversion tool. (We cover customer relationship management [CRM] this week.)

Simon Says: Good professors find ways to make class memorable for their students.

No, this code isn’t terribly complicated and I don’t expect to use it in lieu of our paper-based method of keeping stats. What’s more, unless they work for a sports organization, I don’t expect my students to use anything exactly like this when they graduate. Still, it demonstrates an important programming concept in what I hope is a memorable way.

My favorite professors did the same for me. Why not pay it forward?


What say you?




Filed Under

Enjoy this post? Click here to subscribe to this RSS feed or here to sign up for my bi-monthly newsletter.

Submit a Comment

Your email address will not be published. Required fields are marked *