#π UnboundLocalError: cannot access local variable 'high_scores_saves' where it is not associated wit
63 messages Β· Page 1 of 1 (latest)
@candid quarry
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.
You're reassinging high_scores_saves in the function, so it was compiled as a local variable. You need a global high_scores_saves statement at the top of the function so it's treated as a global.
lets say i dont wanna use global then what do i do?
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.
thats wayy out of scope of what i know about py till now
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
Then a global statement is probably your best bet.
ughhh... okayy imma try to use it as a parameter first, then try that
The parameter/return approach almost certainly won't work if the library is handling calling the function.
I suggested that before seeing that this was P5.
u were correct, it did not
yeah it is mainpy p5.js but in python
Why are you avoiding global? Because you heard that it was bad practice?
difficult to convince sir to let me use it
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.
ahhh okay imma use it then
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.
though can u give a push on how to use it as global
yweahhh imma try to convince him on monday
From a quick scan over the docs, it looks like globals are typical: https://p5.readthedocs.io/en/latest/tutorials/data.html#splitting-and-joining-strings
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.
hmmm alright imma try global
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()
Yep
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.
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```
what now?
You put global in end_game, not draw.
thats what i did, no?
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.
sooo?
The global statement needs to be in the function that's reassigning the global.
And draw is reassigning high_scores_saves.
aha ok
To be clear, global affects the function it's in, not the variable. That's a common point of confusion.
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
Yay
it might be a bit buggy, but it works
That's progress at least.
yesssssssssssss
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?
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?
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.