1

I have this code:

x = 0
def function(x):
    while True:
        x += 1
        if x == 100001:
            print("x = %d" % x)
            return x
            break
function(x)
print("x = %d" % x)

I get this result:

x = 100001
x = 0

Why doesn't it print x = 100001 both times? Shouldn't x have the same value of 100001 in the code outside of the function?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

5 Answers5

3

As Integers are immutable in python, don't expect the function to change it's value.

As you already return x from the function, you just need to catch it like this:

if x == 0:
    x = function(x)

BTW, you can drop the break statement after the return, it's meaningless as you return x one line above it.

omri_saadon
  • 10,193
  • 7
  • 33
  • 58
  • The issue is not caused by integers being immutable in Python (although they are); it is caused by the fact that `x` inside the function is a *local variable* that exists completely separately from the global `x`. – Karl Knechtel Jul 03 '22 at 03:48
2

Because functions have their own local scope. x inside the function is different from x outside. While x variable might be equal to 100001, but in the global scope it is still equal to 0, and hence statement x==100001 is false.

Try returning a boolean value or x from the function (as @omri_saadon) suggested.

Just for educational purposes, and I don't recommend you to do this ever, but you can use global keyword to use same x variable in the function too.

def function(x):
    global x;
    ...

Now your code should work as you initially expected.

hspandher
  • 15,934
  • 2
  • 32
  • 45
0

function(x) is an expression, which returns a value. If you want to save it inside the variable x, you must assign it explicitly. Your code should be:

x = function(x)
print("x = %d" % x)

Moreover, you can greatly simplify your function by putting the test inside the while statement :

def function():
    x = 0
    while (x <= 100001):
        x += 1
    return x

Before knowing very well what you're doing, I don't recommend using break inside a loop.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Thomas Dussaut
  • 728
  • 1
  • 7
  • 20
0

It's due to scoping rules. The x inside your function function overrides the x in the outer scope. It becomes more clear if you rename the parameter to y and assign the function's return value to x as in:

x = 0
def function(y):
    while True:
        print(y)
        y += 1
        if y == 100001:
            return y
            break
if x == 0:
    x = function(x)
if x==100001:
    print("TRUE")
else:
    print("FALSE")
Daniel Jonsson
  • 3,261
  • 5
  • 45
  • 66
0

The value of x is not changing due to the scope reference. Within your function you are referencing a local variable, x, not the global variable x. You would want to set the value of x to the value being returned by your function.

if x == 0:
    x = function(x)

Alternatively, you can use global inside your function to reference the global variable, however, it would be best to not mix the global and local scopes.

Additionally, it is better to not use the same variable declarations for this reason.
It can become confusing when you share variable names and it is very easy to override values when not intended.

Think of scope as a conversation you're having: You have two friends named John, but they are not the same person. If you wish to only talk to one John and not the other, then you declare that by selecting the desired John, in this case the variable x.

When debugging, it is useful to know what you are getting so in the future you should output the actual value of the variable to determine if that value is what you expected it to be.

if x == 0:
    function(x)
print(x) # Prints 0

Added to the modified function call:

if x == 0:
    x = function(x)
print(x) # Prints 100001
Robert
  • 8,717
  • 2
  • 27
  • 34