0

I get this error in my python programme:

Line = (str(Line7) + (" ") + str(Line2))
UnboundLocalError: local variable 'Line7' referenced before assignment

For this part of the code:

Line7 = ("Y Y Y B B B G G G")
Line2 = ("O O O")
def Rotation_Top_Right():
    Line = (str(Line7) + (" ") + str(Line2))
    print(Line)
    Line_List = []
    for i in range(12):
        i -= 3
        Line_List.append(Line.split(" ")[i])
    for i in range(9):
        Line7 = Line_List[i]
    for i in range(3):
        i -= 3
        Line2 = Line_List[i]
Rotation_Top_Right()

Tar in advance.

Tom
  • 1
  • 2
  • Are you sure the code is organized that way? Aren't you calling the function before the `Line7` assignment? – Pablo Antonio Feb 14 '15 at 14:33
  • No, the function is being called after. – Tom Feb 14 '15 at 14:36
  • possible duplicate of [Python 3: UnboundLocalError: local variable referenced before assignment](http://stackoverflow.com/questions/10851906/python-3-unboundlocalerror-local-variable-referenced-before-assignment) – Joe Feb 14 '15 at 14:40

1 Answers1

0

You need to either declare them inside or pass them to the function:

def Rotation_Top_Right(): # inside function
    Line7 = ("Y Y Y B B B G G G")
    Line2 = ("O O O")

def Rotation_Top_Right(Line7,Line2): # use as parameters and pass

You can use global also but I would recommend that you not.

You can also rename the variables in your function that are shadowing the variables from the global namespace as you are actually reassigning and not updating the values:

Line7 = ("Y Y Y B B B G G G")
Line2 = ("O O O")
def Rotation_Top_Right():
    Line = (str(Line7) + (" ") + str(Line2))
    Line_List = []
    for i in range(12):
        i -= 3
        Line_List.append(Line.split(" ")[i])
    for i in range(9):
        Line_7 = Line_List[i] # change to Line_7 
    for i in range(3):
        i -= 3
        Line_2= Line_List[i] # change to Line_2

I would still favour passing in the variables.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321