error handling in user input: is this natural or just laborious

sam python.sam at
Sat Oct 7 01:53:19 CEST 2006

you're right, of course. it occurred to me that, even if it were
manageable for a few items, it would quickly become absurd as the
number of items grew. this:

def get_pct():
	while True:
		pct_list=[['cash', 0], ['bond', 0], ['blue', 0], ['tech', 0], ['dev',
		for i in range(len(pct_list)):
			pct_list[i][1]=input('Please enter the percentage value for %s: '
		if total == 100:
			return pct_list
			print "You've messed up, do it again..."

is much better, and easily maintainable by adding to the assignment for
pct_list. still leaves me with checking for strings though, in case
someone types 'donut' at the prompt...

More information about the Python-list mailing list