#πŸ”’ UnboundLocalError: cannot access local variable 'high_scores_saves' where it is not associated wit

63 messages Β· Page 1 of 1 (latest)

candid quarry
#

how do i make sure that the high score is being updated every time?
right now the program isnt saving the high scores, it always shows as 0 no matter what

https://srcb.in/0KfDj3RWhW

tidal acornBOT
#

@candid quarry

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tardy wolf
candid quarry
tardy wolf
#

Well, high_scores_saves is currently a global, so you'd need to do something like pass it in as a parameter, return the new value from the function, and then reassign the global at the callsite.

#

If draw is a special function called by the library though, you'd probably need to use global.

#

Or, make draw the method of a class (if the library allows that), and make high_scores_saves an attribute of the class.

candid quarry
tardy wolf
#

Actually, you could probably just use a bound method, and the library wouldn't even care.

class State:
    def __init__(self):
        self.high_scores_saves = {}

    def draw(self):
        . . .

state = State()
p.draw = state.draw
tardy wolf
candid quarry
tardy wolf
#

I suggested that before seeing that this was P5.

candid quarry
tardy wolf
#

Why are you avoiding global? Because you heard that it was bad practice?

candid quarry
#

difficult to convince sir to let me use it

tardy wolf
#

Well, like I said, you're already using globals. high_scores_saves is a global in your code. A global statement is just a symptom of that, not the cause.

candid quarry
#

ahhh okay imma use it then

tardy wolf
#

If your instructor has an issue with it, you may want to check with them, but I don't know how else you're supposed to do it unless P5 has its own state-management.

candid quarry
#

though can u give a push on how to use it as global

candid quarry
tardy wolf
#

All those examples use a global state. You could probably use a class like I showed, but if that's beyond what you've done so far, that's likely not an option.

candid quarry
#

hmmm alright imma try global

candid quarry
# tardy wolf All those examples use a global state. You could probably use a class like I sho...

somethign like this:

def end_game():
    global high_scores_saves  # Declare high_scores_saves as a global variable
    # Get the current timestamp
    current_time = time.time()
    # Update the high_scores_saves dictionary with the current score and time
    high_scores_saves[current_time] = p.score
    
    # Reset game state
    p.score = 0
    p.level = 1
    p.lives = 3
    p.game_started = False
    p.showing_high_scores = False  # Ensure high scores screen is not active
    draw_start_screen()
tardy wolf
#

Wait

#

p.showing_high_scores = False

#

If you can do that, you could also presumably just attach high_score_saves to p as well. I'm guessing.

#

I've never used P5 before. My only experience with it is when I used to watch the Coding Train.

#

Although p is a global, so this isn't really any better.

candid quarry
# tardy wolf Yep

got the same error with that:

Traceback (most recent call last):
  File "main.py", line 171, in draw
    high_scores_saves = end_game(high_scores_saves)  # Call end_game here to save high scores
                                 ^^^^^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'high_scores_saves' where it is not associated with a value```
tardy wolf
candid quarry
tardy wolf
#

It's actually not needed in end_game now that I look again.

#
def end_game():
    global high_scores_saves
#

The error is in draw.

candid quarry
#

sooo?

tardy wolf
#

The global statement needs to be in the function that's reassigning the global.

#

And draw is reassigning high_scores_saves.

candid quarry
#

aha ok

tardy wolf
#

To be clear, global affects the function it's in, not the variable. That's a common point of confusion.

candid quarry
# tardy wolf To be clear, `global` affects the function it's in, not the variable. That's a c...

okay so what im saying is:

def end_game():
    global high_scores_saves  # Add this line to access the global variable
    # Get the current timestamp
    current_time = time.time()
    # Update the high_scores_saves dictionary with the current score and time
    high_scores_saves[current_time] = p.score
    
    # Reset game state
    p.score = 0
    p.level = 1
    p.lives = 3
    p.game_started = False
    p.showing_high_scores = False  # Ensure high scores screen is not active
    draw_start_screen()```

and in the draw()

```py
if p.lives == 0:
    end_game()  # Call end_game here to save high scores
    draw_start_screen()  # Game over, show start screen again
elif not p.game_started:
    draw_start_screen()```

with other conditions as well
#

YO

#

YO

#

YO

#

YOOOOOOOOO

#

IT WOTKS

tardy wolf
#

Yay

candid quarry
#

it might be a bit buggy, but it works

tardy wolf
#

That's progress at least.

candid quarry
candid quarry
#

ah sometimes my brain goes poof

#

i was trying to update my scores by replacing the same key and value with 0

#

😹

#

yeah no, i give up

#

how do i make sure the high score is being saved?

tidal acornBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

πŸ”’ how do i make sure the high score is being saved?

tidal acornBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.