1

I am on the ModelBuilder of ArcGis10.3. I have a While iteration and I want to add a counter which indicates the number of the run at each run. This value can be also useful for in line variable substitution to save/load some data using the number of the run.

enter image description here

What I tried to do is to link the Continue with Calculate Value (2) which is my counter whose value is stored in the Main Value. Calculate Value (2) contains this:

Expression: x(%Continue%)

Codeblock: def x(n):
if n==True: x=x+1

The code should be syntactically ok, because it allows me to run it, but it doesn't recognize "True" as a boolean value and it think it is a variable.

I was using a For iteration before and the counter was already part of the tool. Now with the While cycle I am not even sure if what I am trying to do is the easiest way to create a counter.

1

I have run into similar issues getting boolean values into the calculate tool.

In order to get this to work, you need to double quote the Continue variable in the Expression. This will force the variable to be evaluated as string, so "True" and "False" instead of True and False boolean values.

So

Expression: x("%Continue%")

You can then do something like

n.lower() == "true":

The "lower" is just to make absolutely sure you won't have a case issue in the comparison.

  • that is part of the problem. But how could I create a variable which indicates the number of the run? I have to initialize a variable as 1 and add +1 at every run. But If I just write: 'Expression: x("%Continue%") def x(n): if n== "true": x=x+1' It wouldn't work because I have to initialize the x, but I can't initialize inside the calculate field(2) or it will re-initialize that variable at every run. I really don't know how to build a counter for the While iterator. – ales.singli Nov 7 '16 at 13:40
  • 1
    You are aware of the %n% system variable for iterators, as mentioned on this page in the grey "Note" box?: desktop.arcgis.com/en/arcmap/10.3/analyze/modelbuilder/…. That should do what you want, you don't need to build this yourself. – Marco_B Nov 8 '16 at 22:36

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.