Ballon popper game crashes as soon as I hit a ballon

Please help me fix the error where the pygame window crashes as soon a i hit a balloon in the game
Repl link:

code snippet

 def burst(self):
        global score
        pos = pygame.mouse.get_pos()

        if isonBalloon(self.x, self.y, self.a, self.b, pos):
            score += 1
#more code

just go to the link to get idea what ballon[i] is

Hello, can you be a lot more specific on what the error is?
I tried out the game and it works fine.

1 Like

Hello @NuclearPasta0
Sorry it seems that when i moved it to my ‘archive’ the error occurs there.
the error is that when i have already stated the score beforehand, it says that i have not defined score.
the link is
the modoule is in folder “used” and is called
Thank you.

the reason you are getting this error is because score is no longer a global variable, since it is inside the ballongame function now.
To fix, replace all global keywords with nonlocal. The nonlocal keyword is basically like the global keyword, except it is for variables in the enclosing function scope, instead of the module scope.


thanks it worked.
just confirming, nonlocal is just a smaller version of global?

nonlocal is the same as global except nonlocal only works on the local variables of the “enclosing” function (so the keyword can only be used inside a nested function), and global only works on the top-level, module variables (and this would be used inside any function).

foo = 2
bar = []
def my_function():
    eggs = 'hi'
    global foo
    foo = 5
    def my_nested_function():
        nonlocal eggs
        eggs = 'hello'
        global bar
        bar = {}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.