jeudi 28 octobre 2021

Unable to fathom logic expression for use in if... else... statement Python

I'm attempting a little game and I need to use some logic to check usage on board positions in order to set a bool validMove flag. I appreciate this isn't expert level code and there is certainly a better way of doing it, but this is the code I have written and is what I am using. I need to check for the instance of a space or an underscore at a specific point in a list. When I check for a space the code works, when I check for an underscore the code works, but when I try checking for both conditions with an or statement, the code fails. Could an expert advise what I am doing wrong please. Many thanks.

This code checking for an underscore works...

    while not valid_move:
    # check for unoccupied position
    if board[x][y] != '_':
        valid_move = False
        print('Invalid move detected.')
        print(board[x][y])
        get_user_input()
        getCoordinates(position)
    else:
        print('Valid move...')
        valid_move = True
        # update board with player position
        board[x][y] = player

This same code checking for a space works...

    while not valid_move:
    # check for unoccupied position
    if board[x][y] != ' ':
        valid_move = False
        print('Invalid move detected.')
        print(board[x][y])
        get_user_input()
        getCoordinates(position)
    else:
        print('Valid move...')
        valid_move = True
        # update board with player position
        board[x][y] = player

This code using on or statement checking for both, fails...

    while not valid_move:
    # check for unoccupied position
    if (board[x][y] != ' ') or (board[x][y] != '_'):
        valid_move = False
        print('Invalid move detected.')
        print(board[x][y])
        get_user_input()
        getCoordinates(position)
    else:
        print('Valid move...')
        valid_move = True
        # update board with player position
        board[x][y] = player

Aucun commentaire:

Enregistrer un commentaire