variable assignment error, reference before asignment?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 476 Views - Last Post: 09 September 2013 - 08:37 PM Rate Topic: -----

#1 rippinrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-August 13

variable assignment error, reference before asignment?

Posted 09 September 2013 - 06:13 PM

a = [[00, 01, 02, 03],
    [10, 11, 12, 13],
    [20, 21, 22, 23],
    [30, 31, 32, 33]]
y = 0
x = 0
def movement(direction):   
    if direction == "south":
        y = y + 1
        location = a[y][x]

    if direction == "north":
        y = y - 1
        location = a[y][x]


here is my code I'm testing out.

now i have assigned x and y a value of 0 clearly above my movement(), but when I call my function in my shell after importing this script I get an error that says "UnboundLocalError: local variable 'y' referenced before assignment"

Can anyone please point out what I'm doing wrong? I'm baffled

Is This A Good Question/Topic? 0
  • +

Replies To: variable assignment error, reference before asignment?

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:01 PM

You're trying to increment y before you've initialized it. The y outside of movement() is global. The one inside movement() is local. If you set y = 0 inside movement() above the operations that manipulate it, you'll notice that error will go away.
Was This Post Helpful? 0
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:02 PM

If you are modifying a global variable inside a function, you need to bind it to the function scope using the global keyword. Like:
def movement(direction):
    global y
    ' Rest of code



Read all about that in the docs.
Was This Post Helpful? 1
  • +
  • -

#4 rippinrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-August 13

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:11 PM

Alright thanks man that stopped the error but now i have gotten a new problem

a = [[00, 01, 02, 03],
    [10, 11, 12, 13],
    [20, 21, 22, 23],
    [30, 31, 32, 33]]

def movement(direction):
    x = 0
    y = 0
    location = 0
    
    if direction == "south":
        y += 1
        location = a[y][x]

    if direction == "north":
        y -= 1
        location = a[y][x]

    if direction == "east":
        x += 1
        location = a[y][x]

    if direction == "west":
        x -= 1
        location = a[y][x]

    print location
        










all my variables reset to 0 every time I call movement() and I want them to be stored and not just temp.
Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:15 PM

Then you should follow Atli's advice and declare inside movement() that it is to use the global version of those variables.

def movement(direction):
    global x, y
    location = 0

    if direction == "south":


Was This Post Helpful? 1
  • +
  • -

#6 rippinrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-August 13

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:25 PM

Im sorry guys i have tried all of that and it keeps going in circles. i cant seem to keep the variables updated, they always reset. trying to understand the scopes...
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:28 PM

Are you just trying to keep x and y updated, or location, too? I assumed just x and y since you set location every time movement() is run.
Was This Post Helpful? 0
  • +
  • -

#8 rippinrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-August 13

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:35 PM

Im trying to change x and y depending on if I move north/south for y and east/west for x, and those feed into location giving it an x and y cordinate. the list a[] is like a "map" of sorts
Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:40 PM

Did you remove the x = 0 and y = 0 from the movement() function? If not, it'll reset every time that function runs.
Was This Post Helpful? 0
  • +
  • -

#10 rippinrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-August 13

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:42 PM

a = [[00, 01, 02, 03],
    [10, 11, 12, 13],
    [20, 21, 22, 23],
    [30, 31, 32, 33]]

def movement(direction):
    global x, y, location
    
    
    if direction == "south":
        
        y += 1
        location = a[y][x]

    if direction == "north":
        
        y -= 1
        location = a[y][x]

    if direction == "east":
        
        x += 1
        location = a[y][x]

    if direction == "west":
        
        x -= 1
        location = a[y][x]








tried this but got an error 'NameError: global name 'y' is not defined' when i tried calling movement("south")

i tried putting globals inside the if's and that didnt work, i tried putting globals outside the whole function... i know im doing something wrong and there has to be a very simple answer im overlooking :P
Was This Post Helpful? 0
  • +
  • -

#11 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:48 PM

There is, actually: x and y still have to be defined in global context. You only needed to remove their definition in local context.

Keeping location in the global definition assuming you need to retain its value.

a = [[00, 01, 02, 03],
    [10, 11, 12, 13],
    [20, 21, 22, 23],
    [30, 31, 32, 33]]

x = 0
y = 0
location = 0

def movement(direction):
    global x, y, location

    if direction == "south":
        y += 1
        location = a[y][x]

    if direction == "north":
        y -= 1
        location = a[y][x]

    if direction == "east":
        x += 1
        location = a[y][x]

    if direction == "west":
        x -= 1
        location = a[y][x]


This post has been edited by Valek: 09 September 2013 - 07:48 PM

Was This Post Helpful? 1
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7771
  • View blog
  • Posts: 13,137
  • Joined: 19-March 11

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:56 PM

View Postrippinrob, on 09 September 2013 - 09:42 PM, said:

tried this but got an error 'NameError: global name 'y' is not defined' when i tried calling movement("south")

i tried putting globals inside the if's and that didnt work, i tried putting globals outside the whole function... i know im doing something wrong and there has to be a very simple answer im overlooking :P/>



The global name y isn't defined there. You have a local reference to y which is trying to point to a global, but y doesn't exist at the global scope.
Was This Post Helpful? 0
  • +
  • -

#13 rippinrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-August 13

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 07:58 PM

yes! wow thank you!

Can you elaborate a bit on exactly how that worked?

I read the doc and still didn't understand >.<

sorry i am new to programming, trying to take what iv learned and create a little text game and have had many unforeseen obstacles.
Was This Post Helpful? 0
  • +
  • -

#14 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 08:01 PM

Here it is explained a bit differently

If it still doesn't make sense, explain to me what you do understand, and I can help fill in the gaps for you.
Was This Post Helpful? 0
  • +
  • -

#15 rippinrob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-August 13

Re: variable assignment error, reference before asignment?

Posted 09 September 2013 - 08:09 PM

By looking at the code, im assuming the y and x variables in my ifs are referring to the ones stated as global x,y in my function... and since i declared them global, they look for and refer to global variables declared above all functions.

so there is no point to declare global variables, that are outside of all the functions... because they ARE global... only for ones inside functions to point BACK to ones i declared outside of them...

am i right? close?

and I bookmarked the read you linked, it looks to have some laymen terms.... having to go to bed soon for school tomorrow, I will read it tomorrow.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2