sebastian sebastian - 1 month ago 10
Python Question

value is not defined, even though I clearly did?

The comments in the code provide a lot of the explanation.

For some reason it says that I didnt define "sign_value" even though I did!

This is the error message:

line 38, in check_winner
print(sign)
NameError: name 'sign' is not defined





def check_winner():
# reminder to take win_1 and win_2 in here:
def print_winner(sign):
if sign == "| x ":
print("%s got 3 in a row, %s wins!" % (player_1, player_1))
else:
print("%s got 3 in a row, %s wins!" % (player_2, player_2))
print(sign) # THIS IS LINE 38 -------------------
for i in range(0, 8):
x, y = 0, 0
a, b, c = 0,1,2

if board[x][a] and board[x][b] and board[x][c] == sign:
print_winner()

def move(list):
global turn
global sign # I even said: "global sign"

if turn % 2 == 0: # Here the sign value is set, and I want this value into my print_winner function.
sign = "| x "
else:
sign = "| o "

x_1 = int(input("Type the value of x: "))
y_1 = int(input("Type the value of y: "))
list[y_1 - 1][x_1 - 1] = sign
print_board(list)

check_winner(sign) # It should take the sign_value?

turn += 1
return list

Answer

Your check_winner function doesn't explicitly have any parameters. So even though you are passing the sign_value to it, there is no parameter to explicitly assign that value to in the function definition.

Furthermore sign_value is not defined globally and therefore is not automatically available in the function.

Instead of

def check_winner():

Try

def check_winner(sign_value):

(Although it may be good practice to use a different parameter name in the function to make it clear that it's not a global variable. Otherwise, make it a global variable and don't pass it to the function explicitly.)

For your updated code, you maybe would use

def check_winner(sign):

(Written on my phone and completely untested.)