Solution 1

# Define InvalidSymbolException here
class InvalidSymbolException(Exception):

# Define InvalidPositionException here
class InvalidPositionException(Exception):

def validate_tic_tac_toe_move(symbol, position_x, position_y):
    if symbol not in ('X', 'O'):
        raise InvalidSymbolException()

    valid_positions = (0, 1, 2)  # could have used range(0, 3)
    if position_x not in valid_positions or position_y not in valid_positions:
        raise InvalidPositionException()

Nice Moves

Complete the function validate_tic_tac_toe_move that receives inputs symbol, position_x, and position_y for a move in Tic Tac Toe and checks to make sure each of them are valid.

There are no builtin python exceptions that describe these exceptions meaningfully if one of these move inputs are invalid, so you'll need to create two custom exceptions: InvalidSymbolException and InvalidPositionException.

Remember, all you have to do to create a custom exception is the following:

class ExceptionNameHere(Exception):

You'll learn more about what the keywords class and Exception mean later. You leave the keyword pass in because all we need to do is raise the exception.

For this function, do the following:
Raise InvalidSymbolException if the symbol is not an 'X' or 'O'
Raise InvalidPositionException if the position_x or position_y are not 0, 1, or 2.


validate_tic_tac_toe_move('J', 0, 1) # Raises InvalidSymbolException
validate_tic_tac_toe_move('X', 0, 4) # Raises InvalidPositionException
validate_tic_tac_toe_move('X', 7, 0) # Raises InvalidPositionException
validate_tic_tac_toe_move('O', 1, 2) # Does not raise any exceptions

Test Cases

test invalid position y - Run Test

import pytest

def test_invalid_position_y():
    with pytest.raises(InvalidPositionException):
        validate_tic_tac_toe_move('X', 0, 4)

test invalid position x - Run Test

import pytest

def test_invalid_position_x():
    with pytest.raises(InvalidPositionException):
        validate_tic_tac_toe_move('X', 5, 0)

test invalid symbol - Run Test

import pytest

def test_invalid_symbol():
    with pytest.raises(InvalidSymbolException):
        validate_tic_tac_toe_move('J', 0, 1)
# Define InvalidSymbolException here # Define InvalidPositionException here def validate_tic_tac_toe_move(symbol, position_x, position_y): pass