May-06-2020, 12:55 PM
Hey, I'm new to Python and writing my first program, a simple naughts and crosses.
I have a function check after each move to see if the player making the move has won. If the condition of the if statement is satisfied it changes an external 'win' variable to 'True' but when it comes out of the check function to the next line in my while loop the 'win' variable is changed to 'False' for no reason! If I print 'win' as the last part of the if statement and the next thing in the while loop with no code in between it sill changes. How??
It's like the 'win' variable is internal only to the function, if hat is true how do I get it to change the external 'win'?
Cheers,
Nate
I have a function check after each move to see if the player making the move has won. If the condition of the if statement is satisfied it changes an external 'win' variable to 'True' but when it comes out of the check function to the next line in my while loop the 'win' variable is changed to 'False' for no reason! If I print 'win' as the last part of the if statement and the next thing in the while loop with no code in between it sill changes. How??
It's like the 'win' variable is internal only to the function, if hat is true how do I get it to change the external 'win'?
Cheers,
Nate
print("Welcome to Nate's Naughts and Crosses") player1 = input("Player 1 please enter your name: ") print('Thank you', player1) player2 = input('Now your turn Player 2: ') print('Thank you', player2) print(player1, 'goes first. The table below shows which numbers correspond to which cells.') win = False d = {9:9, 8:8, 7:7, 6:6, 5:5, 4:4, 3:3, 2:2, 1:1} board = '{}|{}|{}''\n''- - -''\n''{}|{}|{}''\n''- - -''\n''{}|{}|{}'.format(d[9], d[8], d[7], d[6], d[5], d[4], d[3], d[2], d[1]) print(board) d = {9:' ', 8:' ', 7:' ', 6:' ', 5:' ', 4:' ', 3:' ', 2:' ', 1:' '} print('When it is your turn please enter a number to make your move.') def p1_move(): print(player1, 'please make your move:') move1 = int(input('(Input a number 1-9): ')) while not move1 in range(1,10): print('Please choose a number between 1 and 9.') move1 = int(input('(Input a number 1-9): ')) while not d[move1] == ' ': print('Please choose a blank square.') move1 = int(input('(Input a number 1-9): ')) d[move1] = 'O' board = '{}|{}|{}''\n''- - -''\n''{}|{}|{}''\n''- - -''\n''{}|{}|{}'.format(d[9], d[8], d[7], d[6], d[5], d[4], d[3], d[2], d[1]) print(board) def p2_move(): print(player2, 'please make your move:') move1 = int(input('(Input a number 1-9): ')) while not move1 in range(1,10): print('Please choose a number between 1 and 9.') move1 = int(input('(Input a number 1-9): ')) while not d[move1] == ' ': print('Please choose a blank square.') move1 = int(input('(Input a number 1-9): ')) d[move1] = 'X' board = '{}|{}|{}''\n''- - -''\n''{}|{}|{}''\n''- - -''\n''{}|{}|{}'.format(d[9], d[8], d[7], d[6], d[5], d[4], d[3], d[2], d[1]) print(board) def win_check(): if (d[9] == 'X' and d[8] == 'X' and d[7] == 'X') or (d[6] == 'X' and d[5] == 'X' and d[4] == 'X') or (d[3] == 'X' and d[2] == 'X' and d[1] == 'X') or (d[9] == 'X' and d[5] == 'X' and d[1] == 'X') or (d[7] == 'X' and d[5] == 'X' and d[3] == 'X') or (d[9] == 'X' and d[6] == 'X' and d[3] == 'X') or (d[8] == 'X' and d[5] == 'X' and d[2] == 'X') or (d[7] == 'X' and d[4] == 'X' and d[1] == 'X'): win = True print('Congratulations: {}! You Win!'.format(player2)) if (d[9] == 'O' and d[8] == 'O' and d[7] == 'O') or (d[6] == 'O' and d[5] == 'O' and d[4] == 'O') or (d[3] == 'O' and d[2] == 'O' and d[1] == 'O') or (d[9] == 'O' and d[5] == 'O' and d[1] == 'O') or (d[7] == 'O' and d[5] == 'O' and d[3] == 'O') or (d[9] == 'O' and d[6] == 'O' and d[3] == 'O') or (d[8] == 'O' and d[5] == 'O' and d[2] == 'O') or (d[7] == 'O' and d[4] == 'O' and d[1] == 'O'): win = True print('Congratulations: {}! You Win!'.format(player1)) while not win: p1_move() print(d) win_check() print(win) if win: break if (d[9] != ' ') and (d[8] != ' ') and (d[7] != ' ') and (d[6] != ' ') and (d[5] != ' ') and (d[4] != ' ') and (d[3] != ' ') and (d[2] != ' ') and (d[1] != ' '): print('No Winner!') break p2_move() print(d) win_check() print(win) if win: break if (d[9] != ' ') and (d[8] != ' ') and (d[7] != ' ') and (d[6] != ' ') and (d[5] != ' ') and (d[4] != ' ') and (d[3] != ' ') and (d[2] != ' ') and (d[1] != ' '): print('No Winner!') break print('GAME OVER')