error handling in user input: is this natural or just laborious
sam
python.sam at googlemail.com
Fri Oct 6 19:53:19 EDT 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',
0]]
total=0
for i in range(len(pct_list)):
pct_list[i][1]=input('Please enter the percentage value for %s: '
%pct_list[i][0])
total+=pct_list[i][1]
if total == 100:
return pct_list
break
else:
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