Joe Strout's

Python Tidbits

Here are some scripts which are small, simple, easy to understand, and which do something at least mildly interesting. Unless noted otherwise, they should run on any platform, and don't require any libraries not included with the standard distribution. (Note to Windows users: run python from a DOS window; do not use PythonWin.)

Scripts may be run in one of the following ways:

  1. drop it onto the Python application icon (on a Mac)
  2. type python (on a command-line system)
  3. type import scriptname within the Python interpreter, then

Dog Years
Lotto Picker
Pig Latin Translator
Mark V. Shaney
Tab Fix
Letters Logic Game
Chat Server
Pattern Generator

Dog Years (

This is a very simple script which calculates your age in dog years. It demonstrates simple console input/output and the if/elif/else construct.

Lotto Picker (

This script, by Manny Juan, generates random numbers appropriate for the California lottery. You should be easily able to customize it for your own favorite money-sink. (No warranty is expressed or implied.)

Warmer/Colder (

This script implements a simple guessing game. The computer picks a random number between 1 and 100, and with each guess (after the first), it tells you whether you're getting warmer or colder. Illustrates looping, random numbers, and other basics.

Questor (

Turn your computer into a super-genius! In this script, you think of an item, and the computer tries to guess it. When it's wrong, you teach it about your new item. After a few dozen games, it starts to get pretty smart! See the sample run to get a feel for it.

This script is a bit longer than the others, but still fairly easy. It demonstrates defining a class and some functions. If you're feeling ambitious, see if you can find a way to save the data and load it back in so that the program doesn't get amnesia every time you quit.

Pig Latin Translator (

This module contains two functions: piglatin(s), which converts a plain English string into Pig Latin, and depiglatin(s), which attempts to do the opposite conversion. (Note that computers are terrible language translators, and some mistakes may be made in translating from Pig Latin to English.) You can run the script alone for a demo, or call these modules from within your own Python programs.

A nice extension of this script would take a text file and translate it entirely to Pig Latin (or vice versa). This might be useful, for example, in making sense of legal documents.

Mark V. Shaney (

This script, by Greg McFarlane, implements the Mark V. Shaney text-generating robot. Given some example text, Shaney will generate random text in a similar style. When given a large collection of messages such as a newsgroup or mailing list digest, it can generate some uncannily lively discussion.

As an example, given the text of the Declaration of Independence, Shaney generated this output. Given a humorous Star Trek script, Shaney's version looked like this.

Tab Fix (

Most text editors on the Mac (and possibly elsewhere) use a tab size equal to 4 spaces. This is very convenient for editing source code. But in the Unix world, many editors can only interpret tabs as being equal to 8 spaces (due to a hardware limitation of the old VT100), so when I share my code with Unix folks, they sometimes complain that the comments and such don't line up neatly, or that the lines go off the edge of the screen after several layers of indentation.

This script is for them! It converts a text file created with 4-space tabs into one usable with 8-space tabs. The result looks exactly the same; even numbers of tabs are halved, and odd tabs are replaced with spaces.

Letters Logic Game (

Here's a logic game coded by Andrew Kuchling ( The computer selects a letter. You ask questions -- e.g., how many horizontals it has (in the capital form) -- to narrow down the possibilities. When you make a guess, the program checks your logic.

Chat Server (

This program demonstrates the use of sockets to create a "chat" server. Launch it on a networked computer; then any number of people can telnet to that computer (port 4000) and exchange messages in (almost) real time. With work, this program could serve as the basis of any multi-user network software.

The server also understands two special commands: quit to disconnect yourself, and shutdown to shut the server down and disconnect all users.

Pattern Generator (

This program, by Marcelino Martins, generates patterns through some kind of one-dimensional cellular automaton (I think!). Anyway, it's fun to watch. It will ask you for several parameters; given the default pattern, the following parameter values give nice results:
Word SizeMin. Val.Max. Val
Last Updated: 10/06/08 . . . . . . Joe Strout