-2

When I start my game and it opens to the menu I keep getting the error. "SyntaxWarning: name 'finishes' is assigned to before global declaration". I doesn't disrupt the game but it is just annoying, any ideas on how to fix it?

def load_level(level):
    walls = []
    players = []
    finishes = []
    x = y = 0
    for row in levels[level]:
        for col in row:
            if col == "W":
                walls.append(Wall((x, y)))
            if col == "P":
                players.append(Player((x, y)))
            if col == "F":
                finishes.append(Finish1((x, y)))
            if col == "G":
                finishes.append(Finish2((x, y)))
            if col == "H":
                finishes.append(Finish3((x, y)))
            if col == "I":
                finishes.append(Finish4((x, y)))
            x += 40.96
        y += 30.72 
        x = 0
    return walls, players, finishes

walls, players, finishes = load_level(currentLevel)


def Menu():
    runnin = True
    while runnin:
        clock.tick(60)
        screen.fill(BLACK)
        mouseclick = pygame.mouse.get_pressed()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(0)
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit(0)
        for option in options:
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.hovered = True
                if mouseclick[0] == 1:
                    if option.text == "Easy":
                        walls, players, finishes = load_level(0)
                        global currentlevel, walls, players, finishes
                        currentlevel = 0
                        main()
                    elif option.text == "Medium":
                        walls, players, finishes = load_level(1)
                        global currentlevel, walls, players, finishes
                        currentlevel = 1
                        main()
                    elif option.text == "Hard":
                        walls, players, finishes = load_level(2)
                        global currentlevel, walls, players, finishes
                        currentlevel = 2
                        main()
                    else:
                        runnin = False
            else:
                option.hovered = False
            option.draw()
        pygame.display.update()
    pygame.quit()
    sys.exit(0)
David
  • 27
  • 8
  • 2
    The issue is this line: `walls, players, finishes = load_level(currentLevel) ` in the global scope which uses the sane name as the `finishes` variable inside the other functions. Just change the variable names here or put it inside a function. – Will Jun 05 '16 at 02:34

1 Answers1

1

This occurs when you assign a value to a variable before calling it global, the way to fix this would be to move the line global currentlevel, walls, players, finishes to the start of the second function rather than in the if statements. The code would still run the same, but would remove the error.

AvahW
  • 2,083
  • 3
  • 24
  • 30