Vale
Vale

Reputation: 31

python variables in a loop

I have a simple code like this one:

def function():
    ciao = stuff + stuff2
    return ciao

def megaf():
    stuff = 1
    stuff2 = 3
    for t in range(10):
        stuff += 1
        stuff2 += 2
        print function()

megaf()

The error I get when I run the code is

NameError: global name 'stuff' is not defined

I don't understand why, since I did define the variable.

I tried defining both variables stuff and stuff2 outside the function but I get the error "UnboundLocalError: local variable 'stuff' referenced before assignment". I can't actually understand what the problem is so I can't even try to figure out a solution for this.

Upvotes: 0

Views: 790

Answers (4)

skyking
skyking

Reputation: 14360

In python variables come in two flavors, local and global. Local variables can only be seen from within the function (and they are different each call to the function) while global can be seen from everywhere (and are persistent).

Also it's possible for having a global and local variable with the same name. If you have a local variable in a function it's the local variable that name refers to.

In order to determine if a variable in a function is local or global you would have to check how it's used. Basically it's assumed to be local unless:

  • It's declared as being global using the global statment
  • It's not being assigned to.

In your first try you assign to stuff and stuff2 at the beginning of megaf so it's considered local (since you didn't declare it as being global). Consequently you can't access them outside megaf.

In your try to fix this by assigning to stuff and stuff2 outside of megaf you still assign to them in megaf making stuff and stuff2 local to megaf. The uses of stuff and stuff2 refers to the local variable, but as you haven't assigned to the local variable before using them (note that the outside assignment to stuff and stuff2 is to the global variable which is different).

There's two workarounds. First the dirty one is to simply declare them as being global:

def function():
    ciao = stuff + stuff2
    return ciao

def megaf():
    global stuff, stuff2

    stuff = 1
    stuff2 = 3
    for t in range(10):
        stuff += 1
        stuff2 += 2
        print function()

this is dirty as it's often regarded as poor practice to use global variables. The other solution is to pass them as parameters to function as mentioned in the other answers.

Upvotes: 0

user5416120
user5416120

Reputation:

You have to add arguments to the function.

def function(stuff, stuff2)

and then when calling the function do :

print function(stuff, stuff2)

This adds the two variables to the function().

Your overall code should look like:

def function(stuff, stuff2):
    ciao = stuff + stuff2
    return ciao

def megaf():
    stuff = 1
    stuff2 = 3
    for t in range(10):
        stuff += 1
        stuff2 += 2
        print function(stuff, stuff2)

megaf()

The function won't read the variables unless you either make global variables or insert arguments.

Upvotes: 0

DJMcMayhem
DJMcMayhem

Reputation: 7669

In python, if you make a variable in a function, that variable will cease to exist at the end of that function.

def foo():
    i = 7

def bar():
    n = i + 7 #This will not work.

What you should do if you want your function to add two numbers together, pass those two numbers as parameters, like this:

def sum(num1, num2):
    return num1 + num2

then in megaf

for t in range(10):
    stuff += 1
    stuff2 += 2
    print sum(stuff, stuff2)

Upvotes: 1

BigZ
BigZ

Reputation: 886

The variables stuff and stuff2 are only known in megaf(). Pass stuff and stuff2 to function() like this:

def function(stuff, stuff2):
     ciao = stuff + stuff2
     return ciao

def megaf():
    stuff = 1
    stuff2 = 3
    for t in range(10):
        stuff += 1
        stuff2 += 2
        print function(stuff, stuff2)
megaf()

Upvotes: 1

Related Questions