I am making a basic tic tac toe game as this is the first time I am coding but I am running into an issue where I define a variable outside the function and try to change it inside but I am unable to think of any alternatives for this it goes like this
win = True
def win_check(sign, player):
if pos1 == pos2 == pos3 == sign or pos4 == pos5 == pos6 == sign or pos7 == pos8 == pos9 == sign:
print_board()
print(f'{player} has won the game')
win = False
elif pos1 == pos4 == pos7 == sign or pos5 == pos2 == pos8 == sign or pos6 == pos9 == pos3 == sign:
print_board()
print(f'{player} has won the game')
win = False
elif pos1 == pos5 == pos9 == sign or pos7 == pos5 == pos3 == sign:
print_board()
print(f'{player} has won the game')
win = False
else:
win = True
Here all I want to do is to change the win to false to stop a while loop when a person wins but the win can't change in the function.
Here is my whole code for a reference:
Player_1 = input('Name of player 1: ')
Player_2 = input('Name of player 2: ')
print(f'{Player_1} is O and {Player_2} is X')
print('Let the game of tic tac toe begin')
pos1 = '1'
pos2 = '2'
pos3 = '3'
pos4 = '4'
pos5 = '5'
pos6 = '6'
pos7 = '7'
pos8 = '8'
pos9 = '9'
turn = 0
pos = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
win = True
def print_board():
print(f'{pos1}|{pos2}|{pos3}')
print(f'-----')
print(f'{pos4}|{pos5}|{pos6}')
print(f'-----')
print(f'{pos7}|{pos8}|{pos9}')
def win_check(sign, player):
if pos1 == pos2 == pos3 == sign or pos4 == pos5 == pos6 == sign or pos7 == pos8 == pos9 == sign:
print_board()
print(f'{player} has won the game')
win = False
elif pos1 == pos4 == pos7 == sign or pos5 == pos2 == pos8 == sign or pos6 == pos9 == pos3 == sign:
print_board()
print(f'{player} has won the game')
win = False
elif pos1 == pos5 == pos9 == sign or pos7 == pos5 == pos3 == sign:
print_board()
print(f'{player} has won the game')
win = False
else:
win = True
while win:
print_board()
if turn % 2 == 0:
placement = input(f'{Player_1} please enter an available slot:')
if pos.count(placement) == 1:
pos.remove(placement)
turn += 1
if int(placement) == 1:
pos1 = 'O'
elif int(placement) == 2:
pos2 = 'O'
elif int(placement) == 3:
pos3 = 'O'
elif int(placement) == 4:
pos4 = 'O'
elif int(placement) == 5:
pos5 = 'O'
elif int(placement) == 6:
pos6 = 'O'
elif int(placement) == 7:
pos7 = 'O'
elif int(placement) == 8:
pos8 = 'O'
else:
pos9 = 'O'
win_check('O', Player_1)
else:
print(f'{Player_1} please try again the entered value is either invalid or already is placed in')
else:
placement = input(f'{Player_2} please enter an available slot:')
if pos.count(placement) == 1:
pos.remove(placement)
turn += 1
if int(placement) == 1:
pos1 = 'X'
elif int(placement) == 2:
pos2 = 'X'
elif int(placement) == 3:
pos3 = 'X'
elif int(placement) == 4:
pos4 = 'X'
elif int(placement) == 5:
pos5 = 'X'
elif int(placement) == 6:
pos6 = 'X'
elif int(placement) == 7:
pos7 = 'X'
elif int(placement) == 8:
pos8 = 'X'
else:
pos9 = 'X'
win_check('X', Player_2)
else:
print(f'{Player_2} please try again the entered value is either invalid or already is placed in')