#πŸ”’ need help understanding error

35 messages Β· Page 1 of 1 (latest)

river plover
#

So I do not understand why I am getting this error : Traceback (most recent call last): File "/storage/emulated/0/Android/data/com.coobbi.python/cache/coobbi/tictactoe/tictactoe.py", line 30, in <module> tictactoe() File "/storage/emulated/0/Android/data/com.coobbi.python/cache/coobbi/tictactoe/tictactoe.py", line 18, in tictactoe print(gameboard) ^^^^^^^^^
UnboundLocalError: cannot access local variable 'gameboard' where it is not associated with a value
1|:/data/user/0/com.coobbi.python/app_HOME $

The following is the full code:

space_one = 1
space_two = 2
space_three = 3
space_four = 4
space_five = 5
space_six = 6
space_seven = 7
space_eight = 8
space_nine = 9
player_one = "X"
player_two = "O"
num_choice_p1 = "Please select a number player one"
num_choice_p2 = "Please select a number player two"
invalid = "Please select a free number space on the board"
gameboard = "1|2|3\n_4_|5|6\n_7_|8|9"
def tictactoe():
print("Welcome to tic tac toe!\nPlease choose players.\nPlayer one is X and goes first, while player two is O.\nEach player takes a turn choosing a number to place their X or O. The first player to get three in a row wins! ")
print(gameboard)
print(num_choice_p1)
while True:
p1_choice = int(input())
if 0 < p1_choice < 10:
p1_choice = str(p1_choice)
game_refresh = gameboard.replace(p1_choice,"X")
gameboard = game_refresh
print(game_refresh)
else:
print(invalid)
print()
tictactoe()

Is this an issue with running on Android? I don't understand why it would say it's not defined with a value.

storm windBOT
#

@river plover

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.

glossy nexus
#

!global

storm windBOT
#
Globals

When adding functions or classes to a program, it can be tempting to reference inaccessible variables by declaring them as global. Doing this can result in code that is harder to read, debug and test. Instead of using globals, pass variables or objects as parameters and receive return values.

Instead of writing

def update_score():
    global score, roll
    score = score + roll
update_score()

do this instead

def update_score(score, roll):
    return score + roll
score = update_score(score, roll)

For in-depth explanations on why global variables are bad news in a variety of situations, see this Stack Overflow answer.

rose scarab
#

in your function tictactoe, you are referring to the local variable gameboard before you're defining it

you defined it outside the function, in the global scope, but function-local variables are determined at compile time, and because you assign to gameboard inside the function, it is therefore a name known to be local in the function - you override this with the global keyword as offby1 says, so it is always known to be global

river plover
#

A few follow ups: 1. doesn't the function pull the value of gameboard, since it was defined before the function? 2. Since I redefined gameboard, doesn't it store the value and update it for the loop?

#
  1. Also how can it pull the value before it's redefined then say after it had no value
rose scarab
#

and if you assign a local, it also does not stay when the function ends

#

as for 3, i don't know what you mean by that

river plover
#

So when I run the code without the line gameboard = game_refresh, it runs fine and pulls the gameboard value and does the replace function

#

That is just with that part taken out.

rose scarab
#

yes, then there's no assignment to gameboard inside the function, so it is not deemed local to the function

#

that's the criterion to make it local to a function: assign to that name

#

did you get it to work with the global keyword suggestion?

river plover
#

I think I understood what you mean and what's happening, but correct me if I'm wrong. So the function is able to pull gameboard for use, but when I redefine it's value in the function it doesn't redefined the variable that came before the function, it just define a variable within the function that ends up having no value.

#

Does that sound right ?

rose scarab
#

yes, that local variable will have no value before it is defined, naturally

river plover
#

So it's like it is a completely different variable. So how do you use the global keyword?

rose scarab
#

yes, it is a completely different variable in a different scope (local vs global scope), you just slap this in your function as the first line and it'll be considered global:

some_name = 0
def f():
    global some_name
    print(some_name)
    some_name += 1
f()
f()```
#

i suppose 'namespace' means something pretty specific, similar to this, but which isn't used in python, so i shouldn't be using that word

river plover
#

Ok, also why is it able to pull the global value, but you can't pull the value to define it locally?

#

Like here

#

def tictactoe():
gameboard = gameboard
print("Welcome to tic tac toe!\nPlease choose players.\nPlayer one is X and goes first, while player two is O.\nEach player takes a turn choosing a number to place their X or O. The first player to get three in a row wins! ")
print(gameboard)
print(num_choice_p1)
while True:

#

Is that just a quirk or precautionary measure for python ?

rose scarab
#

it's a very rigid thing:

  • you assign to the name gameboard in this function, therefore the name is local
  • in gameboard = gameboard, it tries to evaluate the right (expression) side - this local name does not currently have a value, so you get an error
#

i think it's pretty quirky; the name is known to be local before it is assigned

river plover
#

Yea so anytime you put some type of redefine function it then sees it as local only. What is the purpose of the f in def f(): ?

rose scarab
#

just my go-to demonstration function name

#

some people like to say func or test instead

river plover
#

Ok that's what I though lol

#

Thanks for the help

storm windBOT
#
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.