2

I get a Local variable 'first' referenced before assignment error when I run my code.

def start():

    global a
    a = [" "," "," "," "," "," "," "," "," "]
    global first
    first = randrange(2)

    def reverse():

        if first == 1:
            first = 0
        else:
            first = 1

        if first == 1:
            turn = "X"
        else:
            turn = "O"

That is just a part of my code where the error occurs. However when I paste the code into IDLE it works no problem so I don't know why this is happening.

Anyways, my full code (unfinished Tic Tac Toe):

from os import name
from os import system
from random import randrange
from time import sleep




def cls():
    system(['clear','cls'][name == 'nt'])


def start():

    global a
    a = [" "," "," "," "," "," "," "," "," "]
    global first
    first = randrange(2)

    def reverse():

        if first == 1:
            first = 0
        else:
            first = 1

        if first == 1:
            turn = "X"
        else:
            turn = "O"







    while True:

        reverse()
        cls()
        printBoard()
        print ""
        print "Its %s's turn." % (turn)
        print ""
        move = raw_input("Enter your move (1-9): ")

        if move.isdigit() == True:
            move = int(move)

            if move in range(9):
                move = move - 1
                if a[move] == " ":
                    a[move] = turn

                else:
                    print "Incorrect move: Place taken"
                    reverse()
                    sleep(2)    

            else:
                print "Incorrect move: Number out of range"
                sleep(2)    

        else:
            print "Incorrect move: Move not a number"
            sleep(2)







def printBoard():
    cls()
    print a[0],"|",a[1],"|",a[2]
    print       "---------"
    print a[3],"|",a[4],"|",a[5]
    print       "---------"
    print a[6],"|",a[7],"|",a[8]
start()
Ciprum
  • 734
  • 1
  • 11
  • 18
  • Where do you get the error? As In how do you run it when you get the error – letsc Jul 13 '15 at 19:47
  • In `def reverse():` you need to call `global first` again to access it. However, I would suggest against using globals and instead passing values as parameters to the functions. – Brobin Jul 13 '15 at 19:47
  • related: http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them – NightShadeQueen Jul 13 '15 at 19:49

1 Answers1

3

Python scans a function body for any assignments, and if they aren't explicitly declared global, then it creates a local scope variable for that name. Because you assign to first in your reverse() function, and you haven't explicitly declared first to be global within that function's scope, python creates a local variable named first that hides the global one.

It doesn't matter that the assignment comes after the comparison; python implicitly declares all local variables at the beginning of the function.

To fix this you can declare first to be global within the reverse() function, but as others have said, globals should be avoided when possible.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52