mattowen

if function within an while - python novice

Discussion created by mattowen on Feb 25, 2013
Latest reply on Feb 25, 2013 by mattowen
Dear all,

I'm attempting to calculate a field value that is dependent on a number of other fields.

In the following example I'm attempting to calculate values for FIII dependent on the relative values of other F fields.

The issue I have is that there are a large number of '0' values, which in fact signify an absence - I suppose they really ought to be null, but I'm working from a shapefile and I can't nullify the values.

To try and get around this I've tried to incorporate the while function, and only perform the analysis where values are > 0.

def Reclass(FIII, FIA, FIB, FIIA, FIIB, FIVA, FIVB, FIVC):
  Depths = (FIA, FIB, FIIA, FIIB, FIVA, FIVB, FIVC)
  while (Depths > 0):
    if (FIII <= 0):
      return 0
    elif (FIII < min (FIA, FIB, FIIA, FIIB, FIVA, FIVB, FIVC)):
      return FIII
    elif (FIII > max(FIA, FIB)) and (FIII < FIIA):
      return FIII - max(FIA, FIB)
    elif (FIII > FIIA) and (max(FIA, FIB) <= 0):
      return FIII - FIIA
    elif (FIII > min(FIVA, FIVB, FIVC)):
      return (FIII - min(FIVA, FIVB, FIVC))


Is far as I can make out the while function isn't working at all - I assume I just have the syntax incorrect.

Otherwise the major issue is on lines 8 and 9, where FIIA is frequently 0, this means that the condition for the elif statement is never fulfilled, meaning that the return calculation is not performed.

Any pointers gratefully appreciated - apologies for confused thread: I've been staring at the screen all day.

Matt

Outcomes