Python Conditional Statement

Hi all,

I currently have a python script with three solutions; does anyone have any idea on how to link the solutions together please. See attached for context.

Many thanks in advance.
Conditional Statement.gh (5.9 KB)

Sometimes drawing a simple flow diagram might help you with all the ifs. Also, it is not very clear what you are trying to achieve from your example.

Hi, thanks for replying. I have attached the grasshopper file as well.

So the result for HeightM2 in #Solution 1 is actually in #Solution 2 so i want to be able to reference #Solution 2 in #Solution 1 if that makes sense

put the Solution 2 in front of Solution 1 and it should work

edit: You can simplify, if I see it correctly to this single line statement:

a = max([M, R+9, L+9])
3 Likes

Thanks for your suggestion Gijs. I’m still really new to python so can you do me a favour and put the simplified option in context so that I can understand it. Cheers

You have 3 variables, heightR, heightL and heightM
You add 9 to the first 2
Then you take the maximum value of those three resulting variables.
I just simplified the variable names to R L and M and the output variable to a

1 Like

Variables need to be defined.

HeightM, HeightR, and HeightL are defined by the Python Script Component ports.

HeightM2 is not in scope “is not defined” since the code path to define the variable, may not be traveled based on inputs.

HeightM1 = 0  # Defines the variable
HeightM2 = 0  # Defines the variable

You should not receive “not defined” errors but logic or mathematical errors may still exist, I did not review your conditions.

Also, it may be required to have logic where a variable does not exist based on logic path taken by design. There are ways to handle this within the Python language.

1 Like

I forgot to explain that I didn’t include “HeightMOut” or do this:

HeightM1 = 0
HeightM2 = 0
HeightMOut = 0

The reason is that “HeightMOut” is assigned in the “Final Solution” to either “HeightM1” or “HeightM2” which will be assigned lines above.

This is related to what @Gijs replied, order of operation\assignment matters.

Strictly, in Python variables need to be created before any attempt is made to get their data. It is perfectly ok to define a variable “on the fly” by assigning a value to it, as in line 15 of the original script. It is not ok to do what the original script shows in line 18. Your HeightM1 = 0 is not necessary in this context and the HeightM2, if necessary here, would be redundant if the conditional logic was better organised.

There will be instances when predefining variables or instantiating objects before the logic processing commences is necessary, for example to allow a finally block to clean up (but that isn’t a beginner topic!), but generally in Python I would prefer to avoid code bloat.

Anyway, @gijs has presented an elegant solution that renders this moot.

Regards
Jeremy

I agree. I was focused on the compile errors, not the logic. My error.

Thanks Stephen,

This is a good solution as well and your explanation about defining both M1 and M2 was very useful in keeping my definition in order combined with @Gijs simplified solution.

Thanks all, very much appreciated.