[Solved] Return statement not working in Python 3.4.3 [closed]


The program works as intended.

You define global_variable_x = x and give it the name global_variable, but in fact it’s a local variable. The easiest way to be able to keep the data persistent is to modify x or to use classes and write it to a class variable.

To give you some more detailed information:

    x=10

def example():

    global_variable_x = x
    print(" i know i can access it", global_variable_x)
    global_variable_x +=5
    print(" woa, i can modify it" ,global_variable_x)
    return global_variable_x
 example()

It might be abstracted but just to give you some idea:

  • You will put x = 10 on the programming stack.
  • You will call example()

  • The example() function call will create a new stack frame for that
    function call putting global_variable_x on it.

  • When the function call hits the return statement the stack frame will
    be removed and the only thing that remains is x.
  • The second time you run example() it will create a new stack
    frame, put global_variable_x again on it and instantiate it again
    with the value of x, being 10.

The problem here is related to scoping, I suggest you to take a look at: this blog

solved Return statement not working in Python 3.4.3 [closed]