Debugging in Python, part 10: A logical error

MP 151: How do you debug a program that doesn't crash?

In the last two posts we made models called Card, Hand, and Deck, which can form the foundation of any game that uses playing cards. In this post we'll start building a terminal-based implementation of Go Fish. If you haven't played it before, it's a game where you try to get as many pairs of cards as you can, by asking the other player if they have a card you want.

An intentional error

There are a couple logical errors in the models that were developed in the previous posts. Those errors won't show up right away, so I'm going to start with a slight variation of those models, with an error that I've intentionally introduced. This will let us focus right away on how to deal with logical errors while developing a project. The full game will bring us back to the errors I accidentally wrote into the initial implementation.

The version of Card, Hand, and Deck with the intentional error can be found here.

Game specs

Even if you've played Go Fish before, it's good to jot a few specifics about what we're going to build:

  • There will be two players, the human player and the computer. The human player will always go first.
  • When it's your turn, you ask the computer if it has a card that will help you make a pair. If it does, you get that pair, and you go again. The computer will do the same when it takes its turn.
  • Whoever makes the most pairs wins.
  • We won't worry about the computer playing well now, we'll just make sure the computer plays according to the rules.
  • At the end of the game, the player will be given the choice of whether to play again, or quit.

That should be enough to let us start coding an implementation of Go Fish.

Starting the game

We'll write the game in a single file called go_fish.py, and the game as a whole will be represented by a class called GoFish:

This post is only available to paid subscribers at the moment. It will be available to everyone 6 weeks after posting. If you'd like to continue reading now, please support my work by signing up for a paid subscription.

Paid subscription options