dynamic names

Val Bykoski val at vtek.com
Sun Mar 7 19:20:25 EST 2004


val at vtek.com (Val Bykoski) wrote in message news:<a312aab6.0403071126.45582cea at posting.google.com>...
> I'm currently in a familiar to many business of filing my tax
> return.  Being a big fan of Python and to make the process less painful, 
> i started by writing my favored schedules A,B,C, and the most favored 
> form 1040 in Python.  Everything was a real fun until i got to the
> very famous motif "add lined Lx to Ly".
> This is my naive (master)piece:
> 
> def sumL(L1,L2):
>    # sum up a range of Ls
>    tot=0.
>    for L in range(L1,L2+1): 
>       #nL=exec("L%s" % L)
>       tot=tot + ("L%s" % L)
>    return tot
> 
> pythonw -u "sumL.py"
> Traceback (most recent call last):
>   File "sumL.py", line 11, in ?
>     sumL(1,4)
>   File "sumL.py", line 7, in sumL
>     tot=tot + ("L%s" % L)
> TypeError: unsupported operand type(s) for +: 'float' and 'str'
> >Exit code: 1
> 
> Any help would be life-saving.
> 
> let's-make-python-a-tax-pain-killer-ly y'rs
> val

It took me 'just' one hour to get 'a' working code: 

def sumL(fromLine=1,toLine=4, dir=[]):
    #
    tot=0.
    
    for name in dir:
        if name.startswith ("L") and int(name[1:]) in range(fromLine,toLine):
            tot=tot+float(eval(name)) 
            print tot
    return tot        

L1=1.; L2=2.; L3=3.
print dir()
print sumL(dir=dir())

The point is that in the tax forms the number of lines to be added is variable,
and i it's a pain to put them in a list. 

Robert, thanks for your help, appreciate very much.
relieving-ly y'rs



More information about the Python-list mailing list