Skip to content

Scrabble Problem

You're developing a knock-off Scrabble game. You've implemented a Scrabble class that initializes with a specific list of characters, ['s', 'a', 'r', 'b', 'k', 'r', 'e']. It has a play_word(self, word: str) method that doesn't do anything, but it does throw an error if the word you try to play can't be formed from the list of letters.

For example,

  1. Instantiate a new Scrabble game

    scrabble = Scrabble()
    print(scrabble.letters)  # ['s', 'a', 'r', 'b', 'k', 'r', 'e']
  2. Play a valid word

    scrabble.play_word('bark')  # returns None
  3. Play an invalid word

    # ScrabbleWordException: You can't form the word 'meatloaf' with the letters ['s', 'a', 'r', 'b', 'k', 'r', 'e']

Write a test to confirm that playing an invalid word raises the correct ScrabbleWordException.

Directory Structure



class ScrabbleWordException(Exception):
    """Custom Scrabble Exception"""

class Scrabble():
    Fake Scrabble, where the tiles don't have points,
    there's no wildcard tiles, and your letters are

    def __init__(self):
        self.letters = ['s', 'a', 'r', 'b', 'k', 'r', 'e']

    def play_word(self, word: str):
        Play a word

        should be any string formed from the characters in self.letters

        :param word: a string formed from the letters in self.letters
        :return: True if word can be formed from self.letters

        letters = self.letters.copy()
        for char in word:
            except ValueError as e:
                raise ScrabbleWordException(f"You can't form the word '{word}' with the letters {self.letters}")
def test_invalid_word():
    If the user plays an invalid word,
    make sure the right exception is raised.

    # your code here

See the solution