Nested If else [SOLVED]

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
juanson
Posts: 10
Joined: Tue Dec 11, 2012 3:11 am

Nested If else [SOLVED]

Post by juanson » Fri Aug 15, 2014 3:25 am

hi! i'am havign some trouble with this code:

Code: Select all

if eg.globals.Dashboard_X1 == 0:
    if eg.globals.Dashboard_X2 == 0:
        if eg.globals.Dashboard_X3 == 0:
            if eg.globals.Dashboard_X4 == 0:
                    eg.TriggerEvent("SecuenCIx393hcReset")
            else:
                eg.TriggerEvent("SecuenCIx393hc8a")
        else:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc4a")
            else:
                eg.TriggerEvent("SecuenCIx393hc12a")
    else:   
        if eg.globals.Dashboard_X3 == 0:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc2a")
            else:
                eg.TriggerEvent("SecuenCIx393hc10a")
        else:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc6a")
            else:
                eg.TriggerEvent("SecuenCIx393hc14a")
else:
    if eg.globals.Dashboard_X2 == 0:
        if eg.globals.Dashboard_X3 == 0:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc1a")
            else:
                eg.TriggerEvent("SecuenCIx393hc9a")
        else:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc5a")
            else:
                eg.TriggerEvent("SecuenCIx393hc13a")
    else:
        if eg.globals.Dashboard_X3 == 0:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc3a")
            else:
                eg.TriggerEvent("SecuenCIx393hc11a")
        else:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc7a")
            else:
                eg.TriggerEvent("SecuenCIx393hc15a")
It allways Trigger the Event "SecuenCIx393hc15a". The actual variables are 1001, so it should trigger the event SecuenCIx393hc9a".

Thanks.
Last edited by juanson on Fri Aug 15, 2014 1:54 pm, edited 1 time in total.

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Nested If else

Post by krambriw » Fri Aug 15, 2014 5:35 am

If I run the script below it works ok for me

Code: Select all

07:34:17   Python Script
07:34:17   Main.SecuenCIx393hc9a

Code: Select all

eg.globals.Dashboard_X1 = 1
eg.globals.Dashboard_X2 = 0
eg.globals.Dashboard_X3 = 0
eg.globals.Dashboard_X4 = 1

if eg.globals.Dashboard_X1 == 0:
    if eg.globals.Dashboard_X2 == 0:
        if eg.globals.Dashboard_X3 == 0:
            if eg.globals.Dashboard_X4 == 0:
                    eg.TriggerEvent("SecuenCIx393hcReset")
            else:
                eg.TriggerEvent("SecuenCIx393hc8a")
        else:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc4a")
            else:
                eg.TriggerEvent("SecuenCIx393hc12a")
    else:   
        if eg.globals.Dashboard_X3 == 0:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc2a")
            else:
                eg.TriggerEvent("SecuenCIx393hc10a")
        else:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc6a")
            else:
                eg.TriggerEvent("SecuenCIx393hc14a")
else:
    if eg.globals.Dashboard_X2 == 0:
        if eg.globals.Dashboard_X3 == 0:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc1a")
            else:
                eg.TriggerEvent("SecuenCIx393hc9a")
        else:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc5a")
            else:
                eg.TriggerEvent("SecuenCIx393hc13a")
    else:
        if eg.globals.Dashboard_X3 == 0:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc3a")
            else:
                eg.TriggerEvent("SecuenCIx393hc11a")
        else:
            if eg.globals.Dashboard_X4 == 0:
                eg.TriggerEvent("SecuenCIx393hc7a")
            else:
                eg.TriggerEvent("SecuenCIx393hc15a")

User avatar
Pako
Plugin Developer
Posts: 2290
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Nested If else

Post by Pako » Fri Aug 15, 2014 5:58 am

juanson wrote:It allways Trigger the Event "SecuenCIx393hc15a". The actual variables are 1001, so it should trigger the event SecuenCIx393hc9a".
Try adding the following lines just before the code that you showed us.

Code: Select all

print eg.globals.Dashboard_X1
print eg.globals.Dashboard_X2
print eg.globals.Dashboard_X3
print eg.globals.Dashboard_X4
So you will find that the bug is somewhere else. I suppose it is when you parse a kind of variable and converting it to eg.globals.Dashboard_X1 to eg.globals.Dashboard_X4. If I am right, then the question arises here:
really you need those variables eg.globals.Dashboard_X1 to eg.globals.Dashboard_X4 ?
Suppose you have an original variable, let's call her Dashboard.
This is probably the integer that takes values ​​from 0 to 15.
This will be best to use a dictionary:

Code: Select all

table = {
    0:"Reset",
    1:"8a",
    2:"4a",
    3:"12a",
    4:"2a",
}
Fill it yourself.
Then you can simply use

Code: Select all

eg.TriggerEvent("SecuenCIx393hc" + table[Dashboard])
And that's it.
Notes:
#1)If it is necessary to use a global variable, then, of course, instead of table use eg.globals.table.
#2)If you do not have the variable Dashboard, then you can simply get it as follows:

Code: Select all

Dashboard = 8*int(eg.globals.Dashboard_X1)+4*int(eg.globals.Dashboard_X2)+2*int(eg.globals.Dashboard_X3)+int(eg.globals.Dashboard_X4)
Pako

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: Nested If else

Post by krambriw » Fri Aug 15, 2014 7:13 am

Pako, unbelievable, I was just at the same time as you again thinking how this could be solved more elegant...

In Python there is no switch like in C++ so it is, as you already wrote, good to use dictionaries. The script below is an example that I think gives a better overview than those nested if statements that you easily get lost in when you try to debug.

Funny, we where thinking in similar direction but of course, your solution was again more elegant!!!

Anyway, I put my proposal below, it has one advantage, it is very easy to see what you get and it is easy to maintain if you need to change or extend. Also if you need to add some functionality later to each condition, you can simply extend the individual functions.

My best regards, Walter

Code: Select all

 
def oooo():
    eg.TriggerEvent("SecuenCIx393hcReset")
 
def oool():
    eg.TriggerEvent("SecuenCIx393hc8a") 
    
def oolo():
    eg.TriggerEvent("SecuenCIx393hc4a")
 
def ooll():
    eg.TriggerEvent("SecuenCIx393hc12a") 

def oloo():
    eg.TriggerEvent("SecuenCIx393hc2a")
 
def olol():
    eg.TriggerEvent("SecuenCIx393hc10a") 
    
def ollo():
    eg.TriggerEvent("SecuenCIx393hc6a")
 
def olll():
    eg.TriggerEvent("SecuenCIx393hc14a") 
    
def looo():
    eg.TriggerEvent("SecuenCIx393hc1a")
 
def lool():
    eg.TriggerEvent("SecuenCIx393hc9a") 
    
def lolo():
    eg.TriggerEvent("SecuenCIx393hc5a")
 
def loll():
    eg.TriggerEvent("SecuenCIx393hc13a") 
    
def lloo():
    eg.TriggerEvent("SecuenCIx393hc3a")
 
def llol():
    eg.TriggerEvent("SecuenCIx393hc11a") 
    
def lllo():
    eg.TriggerEvent("SecuenCIx393hc7a")
 
def llll():
    eg.TriggerEvent("SecuenCIx393hc15a") 

options = {
    '0000' : oooo,
    '0001' : oool,
    '0010' : oolo,
    '0011' : ooll,
    '0100' : oloo,
    '0101' : olol,
    '0110' : ollo,
    '0111' : olll,
    '1000' : looo,
    '1001' : lool,
    '1010' : lolo,
    '1011' : loll,
    '1100' : lloo,
    '1101' : llol,
    '1110' : lllo,
    '1111' : llll
}
 
#This part is just for testing and can be removed in your application
######################################################
eg.globals.Dashboard_X1 = 1
eg.globals.Dashboard_X2 = 1
eg.globals.Dashboard_X3 = 0
eg.globals.Dashboard_X4 = 1
######################################################

data = ''
data += str(eg.globals.Dashboard_X1)
data += str(eg.globals.Dashboard_X2)
data += str(eg.globals.Dashboard_X3)
data += str(eg.globals.Dashboard_X4)

options[data]()

juanson
Posts: 10
Joined: Tue Dec 11, 2012 3:11 am

Re: Nested If else

Post by juanson » Fri Aug 15, 2014 1:53 pm

Thank you guys!! it helped me to open my mind because i'am clearly an amateur.
But i solved it by adding the coulons to the 0 so now it's "0" instead of 0

that solved the problem.

Thanks again!!!!!!!!!!! :D :D :D :D :D :D :D

Post Reply