Newbie help looping/reducing code

Lance Hoffmeyer lance at augustmail.com
Mon Feb 19 18:10:16 EST 2007


Hey all,


Can someone help me reduce this code?  It sure seems
like there ought to be a way to loop this or combine
things so that there is only 1 or 3 lines to this
instead of 6.  I've been scratching my head over this
for a while though I can't come up with anything.
Just as a note, I need even_odd_round left alone because
it is not always used.  I just included for clarity.

As always, thanks in advance,

Lance

T2B = even_odd_round(float(str(T2B)))	
VS  = even_odd_round(float(str(VS)))	
SS  = even_odd_round(float(str(SS)))	
sh.Cells(21,lastcol+1).Value = float(str(T2B))/100
sh.Cells(22,lastcol+1).Value = float(str(VS))/100
sh.Cells(23,lastcol+1).Value = float(str(SS))/100



def even_odd_round(num):
	if(round(num,2) + .5 == int(round(num,2)) + 1):
		if num > .5:
		     if(int(num) % 2):
		     	num = round(num,2) + .1 #an odd number
		     else:
		     	num = round(num,2) - .1 #an even number
		else:
		   num = 1
	rounded_num = int(round(num,0))
	return rounded_num



More information about the Python-list mailing list