1

I'm trying to use Calculate Value in Modelbuilder's Arcmap to run a process if one of two booleans preconditions is true. So if both of precondintion are false the process shouldn't run, otherwise it should run. First I used the following python code:

Expression: pergunta("%parameter1%","%parameter2%")
Code Block: def pergunta(par1,par2):
                import arcpy
                if par1 == "false" and par2=="false":
                               return "false"

This returned false every time, even when it was supposed to be true. So I changed the code block for the following:

def pergunta(par1,par2):
                import arcpy
                if par1 == "false" and par2=="false":
                               return "false"
                else:
                               return "true"

In this case it returned true every time, even when it supposed to be false.

My last attempt for the code block was the following:

def pergunta(par1,par2):
                import arcpy
                if par1 == "false" and par2=="false":
                               return "false"
                elif par1 == "true" and par2=="false":
                               return "true"
                elif par1 == "false" and par2=="true":
                               return "true"
                elif par1 == "true" and par2=="true":
                               return "true"

Once again, it returned false every time, even when it was supposed to be true.

It seems like no matter what I do, it is not considering if the preconditions are false or true. What am I doing wrong?

2
  • 1
    All your elif statements could be replaced with a single else: return True. I expect the if fails due to incorrect type. Of course all of this is basic Python programming.
    – Vince
    Oct 31, 2019 at 19:19
  • In my second attempt I used else instead of all the elifs. After that, I used the elifs just to test because it was not returning what I expected. In all case it was returning false (attempt 1 and 3) or true (attempt 2) with no correlation to the actual precondition which are booleans parameter from the tool. Do you think it is something wrong with the booleans preconditions? Oct 31, 2019 at 19:33

1 Answer 1

1

You do not describe how your Boolean's are created or initialised, I suspect the issue is there. Below is a simple model with a Calculate Value tool doing what you are trying to achieve, note I have two Boolean variables which I have added and renamed. By the fact I have done nothing to them I am assuming they are FALSE. But look at the processing log, they are being sent to the Calculate Value tool as empty strings...

Model

So by opening the Boolean's and ticking them on then off, they initialise as FALSE, now look at the processing log:

Model updated

Finally ensure your Calculate Value tool is set up correctly:

Calculate Value tool

Your Answer

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

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