[Tutor] Need to be taught a trick or two about printing in neat columns

Dick Moores rdm at rcblue.com
Thu Nov 2 10:32:47 CET 2006


I'm working on a program named unitConversion.py. In it, I give the 
user a chance to see a list of all the measurement units the program 
handles, and their abbreviations. The user needs the abbreviations to 
do the conversions. So I have a couple of functions (see below) that 
together will print a list of unit abbreviations and their full names 
(but not their respective categories). At this point there are 46 
units, and that's a long list for the user to see if the printing is 
done one unit per line. What I'd like to do is print the list in 
maybe 3 neat columns. I'm sure this is possible, but after hours of 
experimentation, I can't get a neat one to print. I know I could 
stick a neat 3-column one in the script, but I'd like to learn how it 
could be done with just the two functions 
allUnitsAndTheirAbbreviationsAndCategories() and a modification of 
printListOfAllUnitsAndAbbreviations().

BTW allUnitsAndTheirAbbreviationsAndCategories() is used elsewhere in 
the program, so I don't want to change it, if possible.

Thanks very much,

Dick Moores

def allUnitsAndTheirAbbreviationsAndCategories():
	"""
	A list of all units, their abbreviations and categories.
	"""
	abbs = [
	'l mi: mile',
	'l km: kilometer',
	'l m: meter',
	'l yd: yard',
	'l ft: foot',
	'l in: inch',
	'l cm: centimeter',
	'l mm: millimeter',
	'l fur: furlong',
	'l lea: league',
	'l nm: nautical mile',
	'a ha: hectare',
	'a ac: acre',
	'a mi2: square mile',
	'a km2: square kilometer',
	'a m2: square meter',
	'a yd2: square yard',
	'a ft2: square foot',
	'a in2: square inch',
	'a cm2: square centimeter',
	'w kg: kilogram',
	'w lb: pound',
	'w gm: gram',
	'w oz: ounce',
	'v qt: quart',
	'v oz: ounce',
	'v l: liter',
	'v ml: milliliter',
	'v gal: gallon',
	'v tbsp: tablespoon',
	'v tsp: teaspoon',
	'v impgal: Imperial Gallon',
	'v yd3: cubic yard',
	'v m3: cubic meter',
	'v ft3: cubic foot',
	'v mi3: cubic mile',
	'v km3: cubic kilometer',
	't F: Fahrenheit',
	't C: Celsius',
	't K: Kelvin',
	's mph: miles per hour',
	's knots: knots',
	's mps: miles per second',
	's fps: feet per second',
	'd deg: degree',
	'd rad: radian'
	]
	return abbs

def printListOfAllUnitsAndAbbreviations():
	"""
	Prints a list of all units and their abbreviations, but not their categories.
	"""
	lstAll = allUnitsAndTheirAbbreviationsAndCategories()
	for x in lstAll:
		print x[2:]
	print



More information about the Tutor mailing list