Defining variables outside a function

Hey,

On Exercise 33, when trying to convert a while loop into a function if I do this

numbers = []
i = 0

def while_loop(limit):
    while i < limit:
        numbers.append(i)
        i += 1

while_loop(6)

I get an error that says that i is referenced before assignment. I can fix this by defining “i = 0” inside the defined function. But I’m wondering why this is so? It seems that Python is fine with me defining “numbers = []” outside the function definition. Why am I getting this error?

Interesting. I’m playing around using your example and many thoughts has come to mind.
The main thing here is related to local and globals variables (Scope). For example, any variable defined inside a function is called local. Any outside this Scope is called global.

numbers = []
i = 0

def while_loop(limit):
    global i
    while i < limit:
        numbers.append(i)
        i += 1
        print(i)

while_loop(6)

This will work, but still looks confusing for me and now I have so many questions. For example:

globalVariable = "This is a global variable"

def globalScope():
    print(globalVaribale)

Whithout global, Python has interpreted the variable i as local,

2 Likes

Hi @pcosta.

I saw the answer from @funception before he removed it.
It was correct.
And as you also suggest: The variable i needs to be inside the function. Below def …(): with the same indentation as “while… :”
You can also print out numbers. Below “i += 1”.
Try out which indentation works best for your desired output.

Thanks a lot. I went a bit further and realized that if I defined i as “global”, then it’s value would carry on. I guess my main question was why the list is handled differently. For the list, I can define it in the beginning, and nothing goes wrong. Are lists automatically “global”, and variables are not?

Hi @pcosta.

I just learned this today from @funception before he deleted his answer.

He wrote that “numbers” which is a variable assigned to a empty list in your example. This variable outside (above) your function is a global variable.
The “i” when you put it inside the function became a local variable.

Thank you @funception for your good explanation while it was up in this thread.
I was lucky to read it while “online”
I hope I did not missunderstood it.

Hello, @ulfen69

Yeah, I was trying to help him, but I’m still a beginner. I don’t know what I did wrong, but suddenly I missed all the code I was about to edit using the ‘code’ and ‘pre’ tags.

If I do something like this:

x = 5

def isGlobal():
    print(x)

# The output when calling isGlobal() will be 5. So far so good.
# And...

x = 5
def isGlobal():
    while x:
        print(x)
        x -= x

Then I got an UnboundLocalError: local variable 'x' referenced before assignment. 
Maybe is something related with the while loop. 
I don't know the answer right now, but eventually we will figure.

Hi @funception.

I am neither a expert. But so far I have noticed that the function can get the global variable and print it.
But the loop needs a local variable inside the function to work.

x = 5

def is_global():
    print(x)


def is_local():
    y = 5
    # you can also use the global here like this:
    # y = x
    while y:
        print(y)
        y -= 1

is_global()
is_local()

Yeah I’m able to fix the issue by either defining i = 0 inside the loop. Or doing this

numbers = []
i = 0

def while_loop(limit):
    global i
    while i < limit:
        numbers.append(i)
        i += 1

while_loop(6)

Was surprised me is that it’s okay to define “numbers” outside, but not “i”. I don’t really know yet why I don’t have to define numbers here as global.

2 Likes

Thank you so much for all posts in this thread @pcosta and @funception.
I guess we are quite unexperienced all three of us.
But I have still learned from you anyway.

  • I did not know much about global/local variables.
  • And to use global “variabel-name” inside a function to reach a variable outside was new to me.

I hope you learned something too.

1 Like

No problem man.
That’s why I’m here: To learn and share (when possible for me).

Have a nice weekend guys.

Yes, that is how you access the i variable outside the function, and also update it. However, take a look and you’ll see you don’t need to put it in the script outside the function, just put it right at the top of the function:

numbers = []

def while_loop(limit):
    i = 0
    while i < limit:
        numbers.append(i)
        i += 1

while_loop(6)

Ok, now that problem’s solved, but what about the numbers variable? Can you put that in too? Well to do that you need to know about return so here’s the code you can study:

def while_loop(limit):
    numbers = []
    i = 0
    while i < limit:
        numbers.append(i)
        i += 1
    return numbers

numbers = while_loop(6)
print(numbers)
1 Like