2

I'm happy to see you. I've recently (2 days ago to be precise) decided to pick python (python 3) as my first programming language. When I started playing around with functions I bumped into a problem I couldn't solve by myself and that's why I decided to ask for advice here.

I've created a simple function that counts from 0 to 100000 that is called when x = 0. The problematic part is that when it finished the function was supposed to return the new x value (which was 100001).

Here's the original code:

x = 0
def function(x):
    while True:
        print(x)
        x += 1
        if x == 100001:
            return x
            break
if x == 0:
    function(x)
if x==100001:
    print("TRUE")
else:
    print("FALSE")

As I expected after compiling the program started counting at 0 and ended at 100000. Despite that it still printed out "FALSE" at the end. I played around with changing numbers and small details, but at the end it still didn't work as intended. Trying to pinpoint the error I reshaped my code into this:

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

After counting at the end of output I got the following lines: x = 100001 x = 0

I think that the something is wrong with my function returning "x" properly. I tried to find the answer myself, but failed. Any ideas?

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
  • 9,513
  • 6
  • 31
  • 58
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
  • 14,790
  • 2
  • 28
  • 43
0

function(x) is a value. If you want to save it inside x, you must affect the result to the value. Your code should be :

if x == 0:
    x = function(x)
if x==100001:
    print("TRUE")
else:
    print("x= %d" % x)

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

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

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

Thomas Dussaut
  • 720
  • 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
  • 2,635
  • 3
  • 39
  • 62
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,600
  • 2
  • 24
  • 34