python calculation error

89
4
06-18-2018 04:25 PM
New Contributor

trying to populate 1 Attribute with values from 1 of 3 Attributes dependent on a value in a 5th Attribute.  Formula is not working out.  Appreciate any help.

def !COUNT_FINA! ( !FLAG! 😞
  if (!FLAG!== ''O'):
      return !LOC_F_OIL!
  elif (!FLAG!== 'G'):
      return !LOC_F_GAS!
  elif (!FLAG!=='B'):
      return !LOC_F_BOTH!

Reply
0 Kudos
4 Replies
Occasional Contributor III

Hi Ursula,

This was posted this to the Survey123 page - however it is more appropriate for the Python‌ group. Can you give more context on where you are running the Python (ArcMap/ArcGIS Pro/arcpy), and what you are looking to achieve. I can point out a few syntax errors as below:

def !COUNT_FINA! ( !FLAG! 😞 - !COUNT_FINA! has to be a function name - it cannot be a variable
   if (!FLAG!== ''O'😞 - the O has two inverted commas before it
      return !LOC_F_OIL!
   elif (!FLAG!== 'G'):
      return !LOC_F_GAS!
   elif (!FLAG!=='B'):
      return !LOC_F_BOTH!

As highlighted above, you have to pass attributes into the function. So you would use something like this as the function:

def CalculateSomething( EvaluateAgainst, ValueToReturn1, ValueToReturn2, ValueToReturn3 ):
   if (EvaluateAgainst == 'O'):
      return ValueToReturn1
   elif (EvaluateAgainst == 'G'):
      return ValueToReturn2
   elif (EvaluateAgainst =='B'):
      return ValueToReturn3‍‍‍‍‍‍‍‍‍‍‍‍‍‍

After which you can pass values into the function by doing this:

CalculateSomething(!FLAG!, !LOC_F_OIL!, !LOC_F_GAS!, !LOC_F_BOTH!)

In ArcGIS Pro you would use this function like this:

ArcGIS Pro Calculate Field with Python Example

Mikie

New Contributor

Thanks for the reply Xander –

Working in Arc 3.1

Have 5 columns:

1 FLAG – has 1 of 3 values – depending on which one tells you which of Attributes 2, 3 or 4 to use

2 if FLAG = O

3 if FLAG = G

4 if FLAG = B

5 COUNT_FINAL – this is the location where a value from Attribute 2, 3 or 4 will reside dependent on what the Flag value was

Reply
0 Kudos
New Contributor

Michael – thanks for the reply – but still missing something

Working in Arc 3.1

Have 5 columns:

1 FLAG – has 1 of 3 values – depending on which one tells you which of Attributes 2, 3 or 4 to use

2 if FLAG = O

3 if FLAG = G

4 if FLAG = B

5 COUNT_FINAL – this is the location where a value from Attribute 2, 3 or 4 will reside dependent on what the Flag value was

Reply
0 Kudos
MVP Esteemed Contributor

Did you really mean ArcView 3.1??

If you are, it doesn't support python.

Perhaps you might wish to clarify exactly what software you are using

Reply
0 Kudos