I am trying to create a little game with python. I have made this code :
def create_ennemy() :
x = 1200
y = randint(0, 650)
t = randint(1, 5)
if t <= 4 :
id1 = c.create_polygon(7.5, 7.5, 7.5, 37.5, 52.5, 22.5, fill='red')
r = 22.5
elif t == 5 :
c.create_polygon(7.5, 7.5, 7.5, 75, 105, 45, fill='red')
r = 45
ennemy_list.append(id1)
ennemy_type.append(t)
But I get the following error :
File "Jeux-d-avions.py", line 71, in create_ennemy ennemy_list.append(id1) UnboundLocalError: local variable 'id1' referenced before assignment
I searched on the internet but it did not solve my problem. Why my code is not working?
In your code, there is no assignment of id1
in the case of elif
I guess what you mean should be as follows:
def create_ennemy() :
x = 1200
y = randint(0, 650)
t = randint(1, 5)
if t <= 4 :
id1 = c.create_polygon(7.5, 7.5, 7.5, 37.5, 52.5, 22.5, fill='red')
r = 22.5
elif t == 5 :
id1 = c.create_polygon(7.5, 7.5, 7.5, 75, 105, 45, fill='red')
r = 45
ennemy_list.append(id1)
ennemy_type.append(t)
There is no issue in your code, you just need to indent your code and a minor change to assign id1
.
def create_ennemy() :
x = 1200
y = randint(0, 650)
t = randint(1, 5)
if t <= 4 :
id1 = c.create_polygon(7.5, 7.5, 7.5, 37.5, 52.5, 22.5, fill='red')
r = 22.5
elif t == 5 :
id1 = c.create_polygon(7.5, 7.5, 7.5, 75, 105, 45, fill='red')
r = 45
ennemy_list.append(id1)
ennemy_type.append(t)
this will fix you mentioned error.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.