1

i know to you my question sounds ridicoulus ... but i ask anyway ;-)

i want to populate a field with a new value.

If the field has a value of 1 it should get the new value of 15. If the field is 0 it should stay 0

So i tried ->

if !Field!=1:
  return 15

That didn' work. I don't know if Show Codeblock should be activated (and if so what to put in there). enter image description here (left part is from the calculate field window and the right part from field calculator windwo)

enter image description here

4
  • ps: im using ArcGis 10.2.2
    – user53534
    Commented Jun 10, 2015 at 14:21
  • Sorry for asking again ( im all new to the Python stuff). I tried what your showed me ... but still ERROR .
    – user53534
    Commented Jun 10, 2015 at 14:25
  • You're trying to calculate a value to itself. Instead of calc(!Value!) in the calculation you need to input a field from which to calculate. That "Value" variable is being pulled from the field, so it would be leave Value in the code block and use calc(!Field!) (where Field is the field you're pulling from). @Evil Genius's answer is totally correct but a little confusing.
    – Wes
    Commented Jun 10, 2015 at 15:48
  • ok i'll try that (alltough first i need to understand haha). its very difficult for me as you see. i posted one more picture (maybe it helps to help me )
    – user53534
    Commented Jun 10, 2015 at 16:08

1 Answer 1

5

You have a couple of issues going on:

First, you should be using == instead of = for comparison in Python.

Also, the code you are trying to write isn't a single expression.
You can either write it into a function in the code block and call that function in the expression:
Code Block:

def calc(field_value):
    if field_value == 1: return 15
    return field_value

Expression:

calc(!Field!)

Or use a shorthand expression: 15 if !Field! == 1 else !Field!

P.S. It's always good to mention what application you are using. I can guess that you are in ArcGIS based on the formatting and use of Code Block, but it's not certain...

2
  • 1
    I think to clarify this answer it would be helpful if you changed the variable name in the function so it's not confused with the Field in the expression.
    – Wes
    Commented Jun 10, 2015 at 15:52
  • 1
    @Wes Agreed, changed it up a bit. Commented Jun 10, 2015 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.