Newbie: can't devide by function()

laotseu bdesth at removethis.free.fr
Mon Mar 24 19:10:47 EST 2003


Sven Brandt wrote:
> Hi,
> 
> I am trying to wirte a script that that first summs up some counters and 
> than calculates the fraction of the total ammount for each object.
> 
> Funktion sum is fine. The funktion percent does not return anything. If 
> I change
> 
> "fraction=i.counter / summ()" to
> "fraction=i.counter * summ()"
> 
> I get a result.

What do you mean 'does not return anything' ? A function always return 
something, be it 'None'.

> Question: Why can I multiply by the funktion sum but not devide by the 
> function sum? (for sum !=0)
> 
> Regards
> Sven
> 
> 
> Script:
> def summ():
> 
> """iterate over some Objects and add up their properties
>      'counter'(type=int) """
> 
>   total_number=0
>   for i in container.objectValues(['Folder']):
>     total_number= total_numer + i.counter
>   return total_number
> 
> 
> def percent():
> 
> """ calculate the fraction of each object """
> 
>   fraction=0
>   for i in container.objectValues(['Folder']):
>     fraction=i.counter / summ()
>   return fraction

I'm not sure of what you're doing here (or is it a typo ?)
The way it is, you'll only get the *last* result...

BTW, you'd better compute 'summ()' only once before iterating.
I can't see the use of computing it on each iteration.

Also, you should have a look at the doc for 'integer division' :

 >>> 2 / 3
0

So if percent() always return 0, this could be your problem...
You could try with :

def percent():

    fraction=0.0
    for i in container.objectValues(['Folder']):
      fraction=i.counter / summ()
    return fraction


It won't correct the other problems, but...

Oh, one last thing : where does the 'container' come from ?


> return fraction()
> 

Err... What is this last line ?

HTH
Laotseu





More information about the Python-list mailing list