I'm learning how to code on Python and here's my first code that I learned from a book. I want to add a score every time the user clicks on an apple.
Here's my error:
line 19, in on_mouse_down
score = score + 1
UnboundLocalError: local variable 'score' referenced before assignment
I already declared a variable on top and every time the user clicks on an apple, I'm incrementing the score by 1. After that, I want to print the score to the console. Here's my code:
from random import randint
apple = Actor("apple")
score = 0
def draw():
screen.clear()
apple.draw()
def place_apple():
apple.x = randint(10, 800)
apple.y = randint(10, 600)
def on_mouse_down(pos):
if apple.collidepoint(pos):
score = score + 1
print(score)
place_apple()
else :
print("You missed!")
place_apple()