Cannot Use Parentheses and Math Logic in ELIF Statements

3 Commentaires

  • Dave Dyson
    Community Moderator

    Hi Jozef B -

    A couple thoughts:

    1. Fwiw, (A + B) / C is not mathematically the same as A + B / C, because multiplication and division operators are resolved prior to resolving addition and subtraction. 
    2. The best practice would be to use parentheses to enclose everything that you want to be resolved, in the order you want them resolved. For your first example:
    ELIF (((A + B) / C) = D)

    You can see this method in the examples here: Nesting multiple IF THEN ELSE functions

  • Jozef B

    Hello Dave,

    1. I did specify that those are mathematically different and will have different results. This is why I mention that the below is a mathematically equivalent workaround.

    A / C + B / C

    2. The example you provided does work for me, and with more testing I found the following combinations to work / not work.

    ELIF (((A + B) / C) = D)     | WORKING
    ELIF ((A + B) / C = D)       | WORKING
    ELIF ((A + B) / C) = D       | NOT WORKING
    ELIF (A + B) / C = D         | NOT WORKING

    I guess it's even weirder because this is not demonstrably required in the initial IF statement - only in the subsequent ELIFs.

    IF (((A + B) / C) = D)       | WORKING
    IF ((A + B) / C = D)         | WORKING
    IF ((A + B) / C) = D         | WORKING
    IF (A + B) / C = D           | WORKING

    Regardless, thank you for clearing this up - I must have been stuck hopping between 3 & 4 without trying another set of parentheses around the entire statement. What constitutes as valid and invalid doesn't seem very predictable here, and I'm glad there's at least a post now showcasing this.

    The link you provided was also helpful as it does specifically state best practices, although in one case it's a best practice and in another it's a requirement.

  • Dave Dyson
    Community Moderator

    Glad I could help, Jozef!


Vous devez vous connecter pour laisser un commentaire.

Réalisé par Zendesk