Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While Loop Help
#1
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



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')
Reply
#2
In your win_check() function, the variable 'win' is local to that function and is not automatically passed out of it. To pass it out, you should return the variable out of the function and then assign it to the outer 'win' variable in your main program.
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'):        
        print('Congratulations: {}! You Win!'.format(player2)) # Print lines come first because function ends when it returns.
        return True
    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'):
        print('Congratulations: {}! You Win!'.format(player1))
        return True
    else
        return False # You MUST return something even if the above conditions fall through.
In your while not win: section replace the win_check() lines with win = win_check().
This will call the win_check() function and assign what it returns to the win variable.
I haven't tried this but it should work. (The second if may need to be an elif?) If not, try to figure it out and reply if you get stuck.

BTW, there is the easy but not recommended method of making the win variable global. Research that option if you like but it's generally a poor choice and will make your code hard to de-bug as it gets more complex.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#3
(May-06-2020, 03:56 PM)Marbelous Wrote: In your while not win: section replace the win_check() lines with win = win_check().
This will call the win_check() function and assign what it returns to the win variable.
I haven't tried this but it should work. (The second if may need to be an elif?) If not, try to figure it out and reply if you get stuck.

That is perfect thank you! Correct on all counts. I have it working now, great advise.

Cheers,

Nate
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020