Using code block in field calculator

1803
11
Jump to solution
03-04-2019 06:24 AM
Thomvan_Oijen
New Contributor II

Hello Community,

I'm completely at a lost here. I have tried all possible combinations and was trying to figure out how to get this to work but it just doesn't work for me.

The thing I am trying to accomplish is the following:

I have 5 fields with a or b answers (1 or 0) and 5 fields with the correct solution. (also 1 or 0).

Q1-5 and A1-5.

I also have 5 fields with a Weight

W1-5.

I am trying to find a floating value of 0-1 based on the answer. This goes in the field Betrouwbaarheid

This is what I have so far, which is to you very clearly incorrect. But I have tried a lot of different types of formatting without avail.

Note: It's updated!

Expression:
calc(!Q1!, !Q2!, !Q3!, !Q4!, !Q5!, !A1!, !A2!, !A3!, !A4!, !A5!, !W1!, !W2!, !W3!, !W4!, !W5)

Code block:
def calc(Q1, Q2, Q3, Q4, Q5, A1, A2, A3, A4, A5, W1, W2, W3, W4, W5):
    if Q1 == A1:
        c1 = 1
    else:
        c1 = 0

    if Q2 == A2:
        c2 = 1
    else:
        c2 = 0
    
    if Q3 == A3:
        c3 = 1
    else:
        c3 = 0
        
    if Q4 == A4:
        c4 = 1
    else:
        c4 = 0

    if Q5 == A5:
        c5 = 1
    else:
        c5 = 0
    
    betr = (c1 * W1 + c2 * W2 + c3 * W3 + c4 * W4 + c5 * W5) / (W1 + W2 + W3 + W4 + W5)
    return betr‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks to the people below a part was resolved. My new code gives no syntax errors.

However, i still get an error when trying to do a field calculation.

 ERROR 000539:   File "<expression>", line 1
    calc(, , , , , , , , , , , , , , )
         ^
SyntaxError: invalid syntax
 Failed to execute (CalculateField).

I hope anyone can shed me some light into how this codeblock programming works. It's a complete and utter mystery to me.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

/blogs/dan_patterson/2016/08/14/script-formatting 

so line numbers can be referred to

The code block shouldn't contain the field names in the exclamation marks.  They should reference variables.

The expression lines makes reference to those.  So the code block with a python parser goes in that section and the expression in its line.  In the example below  !Field1! is now linked to 'a' etc

def fake_def(a,b,c):
    """fake function"""
    d = a + b + c
    return d

# expression

fake_def(!Field1!, !Field2!, !Field3!)

View solution in original post

11 Replies
VinceAngelo
Esri Esteemed Contributor

You haven't defined the Python variables Q1/A1/W1/Q2/A2/W2/Q3/A3/W3/Q4/A4/W4/Q5/A5/W5 (by adding them to parameter list?) and don't use Python syntax to reference the W? variables in the code block.

The expression should look like

computeFn(!var1!,...!varN!)
‍‍

And the code block like

def computeFn(var1,...varN):
     return var1 * ...

- V

DanPatterson_Retired
MVP Esteemed Contributor

/blogs/dan_patterson/2016/08/14/script-formatting 

so line numbers can be referred to

The code block shouldn't contain the field names in the exclamation marks.  They should reference variables.

The expression lines makes reference to those.  So the code block with a python parser goes in that section and the expression in its line.  In the example below  !Field1! is now linked to 'a' etc

def fake_def(a,b,c):
    """fake function"""
    d = a + b + c
    return d

# expression

fake_def(!Field1!, !Field2!, !Field3!)
Thomvan_Oijen
New Contributor II

Thank you! You know this is not explained anywhere.

I'll update on a next post as it's not quite resolved yet.

0 Kudos
Thomvan_Oijen
New Contributor II

Thanks to the people above I finally know how to properly use fields in the code block.

Unfortunately I still get an error message. This time it gives the following error:

 ERROR 000539:   File "<expression>", line 1
    calc(, , , , , , , , , , , , , , )
         ^
SyntaxError: invalid syntax
 Failed to execute (CalculateField).
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

can you copy your function and your expression.

if your function was...

def calc(,,,,,,,,)

and your expression was

calc(,,,,,,)

it will fail miserably since you didn't supply the field names to the expression, nor did you provide variable names to the function

0 Kudos
Thomvan_Oijen
New Contributor II

Thanks dan,

The used expression and code is in my opening post. I updated it.

It has the same amount of variabels in the code as I have fields in the expression. (so same amount of ,)

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

!W5!   you have !W5

VinceAngelo
Esri Esteemed Contributor

Going back and editing the initial post is required in GIS SE, but it's confusing here, where there is no way to distinguish comments  from answers.  Far better to post edited code as a reply to the original post, so that your current code is provided as a snapshot in time.

- V

ScottDrzyzga
New Contributor III

Are all your weight fields (your Ws) stored using numeric data types?  If any (or all) of your weight fields is (are) defined as text strings, then trying to multiply by a string will cause the calculation to fail.