Python Forum
UnboundLocalError: local variable 'figure_perso' referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnboundLocalError: local variable 'figure_perso' referenced before assignment
#1
Hello,
I'm a French user (so my English is bad), and I make a "John Conway Life's Game".
I want to make a function that place where you want custom elements by clicking on the canvas (glider, blinker, and others...), but I'm going to be crazy! Wall

Please, help me, here is the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from tkinter import *
from time import *
largeur = 60
hauteur = 60
cellules = [[0 for j in range(largeur)] for i in range (hauteur)]
voisins = [[0 for j in range(largeur)] for i in range (hauteur)]
 
def compte_voisins(i,j):
    if i == 0:
        if j == 0:
            return cellules[0][1] + cellules[1][1] + cellules[1][0]
        if j == largeur - 1:
            return cellules[0][largeur - 2] + cellules[1][largeur - 2] + \
            cellules[1][largeur - 1]
        return cellules[0][j - 1] + cellules[0][j + 1] + \
        cellules[1][j-1] + cellules[1][j] + cellules[1][j+1]
    if i == hauteur - 1:
        if j == 0:
            return cellules[hauteur - 1][1] + cellules[hauteur - 2][1] + cellules[hauteur - 2][0]
        if j == largeur - 1:
            return cellules[hauteur - 1][largeur - 2] + cellules[hauteur - 2][largeur - 2] + \
            cellules[hauteur - 2][largeur - 1]
        return cellules[hauteur - 1][j - 1] + cellules[hauteur - 1][j + 1] + \
        cellules[hauteur - 2][j-1] + cellules[hauteur - 2][j] + cellules[hauteur - 2][j+1]
    else:
        if j == 0:
            return cellules[i - 1][0] + cellules[i + 1][0] + \
        cellules[i - 1][1] + cellules[i][1] + cellules[i + 1][1]
        if j == largeur - 1:
            return cellules[i - 1][largeur - 1] + cellules[i + 1][largeur - 1] + \
        cellules[i - 1][largeur - 2] + cellules[i][largeur - 2] + cellules[i + 1][largeur - 2]
        else:
            return cellules[i - 1][j -1] + cellules[i - 1][j] + cellules[i - 1][j + 1] + \
                   cellules[i ][j - 1]          +               cellules[i ][j+1] + \
                   cellules[i + 1][j - 1] + cellules[i + 1][j] + cellules[i + 1][j + 1]
 
def grille_voisins():
    for i in range (hauteur):
        for j in range (largeur):
            voisins[i][j] = compte_voisins(i,j)
 
def grille_lendemain():
    for i in range (hauteur):
        for j in range (largeur):
            if cellules[i][j] == 0 and voisins[i][j] == 3:
                cellules[i][j] = 1
            if cellules[i][j] == 1 and voisins[i][j] != 2 and voisins[i][j] !=3:
                cellules[i][j] = 0
 
def affichage_grille(tab):
    for i in range(len(tab)):
        print(tab[i])
 
def jeu():
    dessine_etat()
    for i in range (regle_jours.get()):
        print(" ")
        grille_voisins()
        grille_lendemain()
        sleep(0.1)
        dessine_etat()
 
def dessine_cellule(i,j):
    can.create_rectangle(j * e, i * e,(j + 1) * e - 1,(i + 1) * e - 1, fill="red", width=0)
 
def dessine_etat():
    can.delete("all")
    for i in range (hauteur):
        for j in range (largeur):
            if cellules[i][j] == 1:
                dessine_cellule(i,j)
    fen.update()
 
def trace_quadrillage():
    for i in range(hauteur):
        can.create_line(0, i * e - 1, largeur * e + 1, i * e - 1, fill='black')
    for j in range(largeur):
        can.create_line(j * e - 1, 0, j * e - 1, hauteur * e + 1, fill='black')
 
def action_clic_souris(event):
    b = regle_taille.get()
    f = regle_taillebis.get()
    can.focus_set()
    x = event.x // e
    y = event.y // e
    for c in range(b):
        for d in range(f):
            cellules[y+c][x+d] = 1
            can.create_rectangle((x+d) * e, (y+c) * e,(x+d + 1) * e - 1,(y+c + 1) * e - 1, fill="red", width=0)
 
def action_declic_souris(event):
    b = regle_taille.get()
    f = regle_taillebis.get()
    can.focus_set()
    x = event.x // e
    y = event.y // e
    for c in range(b):
        for d in range(f):
            cellules[y+c][x+d] = 0
            can.create_rectangle((x+d) * e, (y+c) * e,(x+d + 1) * e - 1,(y+c + 1) * e - 1, fill="white", width=0)
 
def create_perso():
    figure_perso = perso_planeur()
    for i in range(int(len(figure_perso)/2)):
        cellules[figure_perso[i*2+1]][figure_perso[i*2]] = 1
        can.create_rectangle(figure_perso[i*2] * e, figure_perso[i*2+1] * e,(figure_perso[i*2] + 1) * e - 1,(figure_perso[i*2+1] + 1) * e - 1, fill="red", width=0)
 
def perso_planeur(type):
    print(type)
    if type == 1:
        figure_perso = [0,1, 1,2, 2,0, 2,1, 2,2]
    elif type == 2:
        figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,2]
    elif type == 3:
        figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,3, 1,2]
    elif type == 4:
        figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,3, 1,3, 0,2]
    return figure_perso
 
def perso_vaisseau_leger():
    figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,2]
    return figure_perso
 
def perso_vaisseau_moyen():
    figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,3, 1,2]
    return figure_perso
 
def perso_vaisseau_lourd():
    figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,3, 1,3, 0,2]
    return figure_perso
 
def action_planeur(event):
    can.focus_set()
    x = event.x // e
    y = event.y // e
    figure_perso = perso_planeur(type)
    print(figure_perso)
    for i in range(int(len(figure_perso)/2)):
        cellules[figure_perso[i*2+1]+y][figure_perso[i*2]+x] = 1
        can.create_rectangle((figure_perso[i*2]+x) * e, (figure_perso[i*2+1]+y) * e,(figure_perso[i*2]+x + 1) * e - 1,(figure_perso[i*2+1]+y + 1) * e - 1, fill="red", width=0)
    return figure_perso
 
e = 10
figure_perso=[]
fen = Tk()
 
can = Canvas(fen, width=600, height=800, bg="white")
can.grid(row=0, column=0, rowspan=6)
 
can.bind("<Button-1>", action_clic_souris)
can.bind("<Button-2>", action_planeur)
can.bind("<Button-3>", action_declic_souris)
 
bouton_lancer = Button(fen, text="Lancer",command=jeu)
bouton_lancer.grid(row=4, column=2)
 
regle_jours = Scale(fen,orient='vertical', from_=5000, to=0, resolution=1,
    tickinterval=500, length=300)
regle_jours.grid(row=3, column=2)
regle_jours.set(5000)
 
regle_taille = Scale(fen,orient='vertical', from_=hauteur, to=0, resolution=1,
    tickinterval=int(hauteur/10), length=300)
regle_taille.grid(row=3, column=3)
regle_taille.set(1)
 
regle_taillebis = Scale(fen,orient='vertical', from_=largeur, to=0, resolution=1,
    tickinterval=int(largeur/10), length=300)
regle_taillebis.grid(row=3, column=4)
regle_taillebis.set(1)
 
bouton_planeur = Button(fen, text="Planeur",command=lambda: perso_planeur(1))
bouton_planeur.grid(row=0, column=3)
bouton_planeur.place(rely=0.1, relx= 0.82)
 
bouton_vaisseau_leger = Button(fen, text="Vaisseau Léger",command=lambda: perso_planeur(2))
bouton_vaisseau_leger.grid(row=0, column=3)
bouton_vaisseau_leger.place(rely=0.14, relx= 0.82)
 
bouton_vaisseau_moyen = Button(fen, text="Vaisseau Moyen",command=lambda: perso_planeur(3))
bouton_vaisseau_moyen.grid(row=0, column=3)
bouton_vaisseau_moyen.place(rely=0.18, relx= 0.82)
 
bouton_vaisseau_lourd = Button(fen, text="Vaisseau Lourd",command=lambda: perso_planeur(4))
bouton_vaisseau_lourd.grid(row=0, column=3)
bouton_vaisseau_lourd.place(rely=0.22, relx= 0.82)
 
dessine_etat()
trace_quadrillage()
fen.mainloop()
The problems come from lines 102 to line 186, ignore the rest. I have this error :

Error:
*** Console de processus distant Réinitialisée *** <class 'type'> Exception in Tkinter callback Traceback (most recent call last): File "C:\EduPython\App\lib\tkinter\__init__.py", line 1538, in __call__ return self.func(*args) File "C:\Users\meder\Downloads\jeu-de-la-vie-2020-06-10-test4.py", line 150, in action_planeur figure_perso2 = perso_planeur(type) File "C:\Users\meder\Downloads\jeu-de-la-vie-2020-06-10-test4.py", line 132, in perso_planeur return figure_perso UnboundLocalError: local variable 'figure_perso' referenced before assignment >>>
What is does mean?

If you want to help me, I think I have a problem with the variables appelation into the functions, the "return", and the buttons...

Thank you. Smile
Reply
#2
It means you are trying to print or use a variable before assigning it.
This is not the best example but, follows along the lines of your error
Example
1
2
print(myvar)
myvar = 'hello'
This would give an error because myvar has not been assigned.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
in the method def action_planeur(event):
there is
1
figure_perso = perso_planeur(type)
type is a bulitin you should have some other variable name here.

when perso_planeur(type) is called with type
1
2
3
4
5
6
7
8
9
10
11
12
def perso_planeur(type):
    print(type)
    if type == 1:
        figure_perso = [0,1, 1,2, 2,0, 2,1, 2,2]
    elif type == 2:
        figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,2]
    elif type == 3:
        figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,3, 1,2]
    elif type == 4:
        figure_perso = [5,0, 6,1, 6,2, 6,3, 5,3, 4,3, 3,3, 2,3, 1,3, 0,2]
        print()
    return figure_perso
type will never be true in any of the if conditions so when it tries to return figure_perso it has not been defined as it would only be defined is one of the condition where true.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  creating arbitrary local variable names Skaperen 9 1,893 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  how solve: local variable referenced before assignment ? trix 5 1,825 Jun-15-2024, 07:15 PM
Last Post: trix
  It's saying my global variable is a local variable Radical 5 5,946 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  local varible referenced before assignment SC4R 6 3,231 Jan-10-2023, 10:58 PM
Last Post: snippsat
  How does UnboundLocalError work? deanhystad 3 2,562 Feb-25-2022, 01:21 AM
Last Post: bowlofred
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 3,040 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  Referenced before assignment finndude 3 4,301 Mar-02-2021, 08:11 PM
Last Post: finndude
  exec + subprocess = UnboundLocalError paul18fr 6 4,895 Feb-04-2021, 06:27 AM
Last Post: Gribouillis
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 16,588 Dec-14-2020, 07:34 PM
Last Post: buran
  Assignment of non-existing class variable is accepted - Why? DrZ 6 6,650 Jul-13-2020, 03:53 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020