0

I would like to run the following pre logic code in the Arcmap field calculator:

def calc(f, fn, f1, f1n)

f= str(f)
f1 = str(f1)

f = f.replace("  ", "")
fn = fn.replace("  ", "")
f1 = f1.replace("  ", "")
f1n = f1n.replace("  ", "")

if f+fn == f1+f1n:
    return "IDENTICAL"
elif f == f1:
    return "Same no diff. street"
elif fn == f1n
    return "diff no. same street"
else
    return "different"

where f and f1 are double and fn and f1n are string type fields. When I run it I get a syntax error for line 1. Could anyone help me spot it? This is the command

calc(!ADDRNMBR!, !ADDRNAME!, !ADDRNMBR_1!, !ADDRNAME_1!)
  • elif fn == f1n: and else: - dont forget the colons - and after def calc(...) – fluidmotion Jul 18 '15 at 18:17
  • yes I fixed that... now I'm getting indentation errors though. What are the indentation rules for Python? – user32882 Jul 18 '15 at 18:26
  • ok - i assumed indentation was there, so i edited your question accordingly. there are docs on indentation here. Basically because python does not use 'end' statements for loops or functions - you indent to form classes, functions, if statements, loops, etc.. – fluidmotion Jul 18 '15 at 18:31
  • @fluidmotion I rolled back the edits because I believe the formatting is part of the OP's coding problem. – Aaron Jul 18 '15 at 18:33
  • thanks @Aaron - i didn't realize that initially. good to roll-back – fluidmotion Jul 18 '15 at 18:34
3

so if it helps - i think it would look something like -

def calc(f, fn, f1, f1n): #note colon here
    # everything within function is indented
    f = str(f)
    f1 = str(f1)

    f = f.replace("  ", "")
    fn = fn.replace("  ", "")
    f1 = f1.replace("  ", "")
    f1n = f1n.replace("  ", "")

    if f+fn == f1+f1n:
        # each return of if or elif is indented
        return "IDENTICAL"
    elif f == f1:
        return "Same no diff. street"
    elif fn == f1n: #and colon here
        return "diff no. same street"
    else: # and colon here
        return "different"
| improve this answer | |
  • thank you that worked and also clarified the use of indents within python – user32882 Jul 18 '15 at 19:54
  • It may help to write your pre-logic code in a Python interpreter first (like IDLE, which comes pre-packaged with ArcGIS Desktop), which takes care of indentation and colon errors, rather than using the plain-text field in the Field Calculator box. It's hard to find errors in plain-text boxes when writing code. – John Jul 18 '15 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.