Python 3 - Unexpected Flow Control when running IF/IF NOT or IF/ELSE

Asked

Viewed 196 times

3

I’m new to Python, but I already have custom Java software.

I would like to better understand why when trying to program a flow control using IF/ELSE or IF/IF NOT the whole block runs.

Python code 3.7

luz_acessa = False
def interruptor():
    global luz_acessa
    if luz_acessa:
        print("True: Luz acessa")
        luz_acessa = False
    if not luz_acessa: // Mesma coisa com 'else' ao invés de 'if not'
        print("False: Luz apagada")
        luz_acessa = True

Console Python 3.7

interruptor()
False: Luz apagada
interruptor()
True: Luz acessa
False: Luz apagada
interruptor()
True: Luz acessa
False: Luz apagada
interruptor()
True: Luz acessa
False: Luz apagada

For example in Java, if I want to invert a Boolean I do something simple like this:

Java code:

private boolean luz_ligada;
    private void interruptor(){
        if(luz_ligada){
            System.out.println("Luz acessa");
            luz_ligada = false;
        } else {
            System.out.println("Luz apagada");
            luz_ligada = true;
        }
    }

Java log in a simple loop:

Luz apagada
Luz acessa
Luz apagada
Luz acessa
Luz apagada

Process finished with exit code 0

2 answers

3


The codes are not equivalent. if not is not the same as else.

In Java, the equivalent of what you wrote in Python is:

private boolean luz_ligada;
private void interruptor(){
    if(luz_ligada){
        System.out.println("Luz acessa");
        luz_ligada = false;
    }

    if(!luz_ligada) {
        System.out.println("Luz apagada");
        luz_ligada = true;
    }
}

To have the same result, you must change the Python code to:

luz_acessa = False
def interruptor():
    global luz_acessa
    if luz_acessa:
        print("True: Luz acessa")
        luz_acessa = False
    else:
        print("False: Luz apagada")
        luz_acessa = True

2

Leonardo Lima’s answer is correct, but only a deeper explanation of why.

Generally speaking, it’s pretty simple. See below the difference of this code:


luz_acessa = False

def interruptor():
    global luz_acessa
    if luz_acessa:
        print("True: Luz acessa")
        luz_acessa = False
    else:
        print("False: Luz apagada")
        luz_acessa = True

for that code:


luz_acessa = False

def interruptor():
    global luz_acessa
    if luz_acessa:
        print("True: Luz acessa")
        luz_acessa = False
    if not luz_acessa:
        print("False: Luz apagada")
        luz_acessa = True

and for a third example not cited above:


luz_acessa = False

def interruptor():
    global luz_acessa
    if luz_acessa:
        print("True: Luz acessa")
        luz_acessa = False
    elif not luz_acessa:
        print("False: Luz apagada")
        luz_acessa = True

The if, is nothing less than a conditional operator at code level, is a branch at the processor level, but focusing on the code level, it aims to verify the veracity of the information placed after it. if these are true it executes the code that follows it.

When using the else a path is created for when the information placed in the first is false, i.e.:


Se a luz estiver acesa:

   Apague

Senão:

   Acenda

Fim Se

In the code you posted, how did not use the operator else for when the information is false, and instead used another operator if which would cause the code to check each one separately and execute them one by one, and not dependently, follows an example of how its code executed:


Se a luz estiver acesa:

   Apague

Fim Se

Se a luz estiver apagada:

   Acenda

Fim Se

This exemplifies, that whenever their function is executed, the two will then perform, causing the first if always be overwritten

As for the example cited by me, the else as already mentioned creates a path for when the information is false, the difference from this to the else if, is that the else if creates a path for when the first information is false, but your information is true, follows example of the execution:


Se a luz estiver acesa:

   Apague

Senão, se a luz estiver apagada:

   Acenda

Fim Se

Supposing that the luz had a third value other than true or false, for example a indefinido, when the light was of value indefinido it would neither be lit nor erased, and to alter that would have to, or put another else if to check when it is undefined, or to place a else at the end of everything so that this is always executed when the previous ones are not.

Anyway, I hope I helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.