Python 3 Scrabble Project: Bonus - Late Player Joins Game (A 'What If?')

This is a question about the Learn Python 3 Scrabble project (https://www.codecademy.com/courses/learn-python-3/projects/scrabble).

In the final step (Step 15), the project suggests for extended practice making a “play_word()” function:

play_word() — a function that would take in a player and a word, and add that word to the list of words they’ve played

When I first read it, for some reason I forgot how Scrabble was played and tried to code it so the function would add a NEW Player, along with the word they played. I failed at this before realizing my interpretation error and writing the correct code to solve the original problem:

def play_word(player, word):
  player_to_words[player].append(word)
  update_point_totals(player, word)

My question is:
If one was to change the above function in this exercise so it would either add a new word to an existing player key’s list of values OR create a NEW player key and a NEW values list to include that new word, how would one go about doing that?

Thanks!

The words should not be keys because that would negate same word used or occurring twice or more. The words should be in a list since it is both mutable and permits duplicates.

The words would already be in a list stored as value to the player key. Step 9 in the project has the user create the “player_to_words” dictionary:

player_to_words = {"player1": ["BLUE", "tennis", "EXIT"], 'wordNerd': ['EARTH', 'EYES', 'MACHINE'], 'Lexi Con': ["ERASER", 'BELLY', 'HUSKY'], 'Prof Reader': ['ZAP', 'COMA', 'PERIOD']}

Sorry that I didn’t include the full exercise code in the original post, I wrongly assumed folks would have already worked through the whole project, but in retrospect that’s a waste of experts’ time. For ease, the full script at the point of the original question (Step 15) would be something like:

letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 4, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]

letter_to_points = {k:v for k,v in zip(letters,points)}

letter_to_points[" "] = 0
print(letter_to_points)

def score_word(word):
  point_total = 0
  for letter in word:
    point_total += letter_to_points.get(letter.upper(), 0)
  return point_total

player_to_words = {"player1": ["BLUE", "tennis", "EXIT"], 'wordNerd': ['EARTH', 'EYES', 'MACHINE'], 'Lexi Con': ["ERASER", 'BELLY', 'HUSKY'], 'Prof Reader': ['ZAP', 'COMA', 'PERIOD']}

player_to_points = {}

for player, words in player_to_words.items():
  player_points = 0
  for word in words:
    player_points += score_word(word)
  player_to_points[player] = player_points

def update_point_totals(player, word):
  player_to_points[player] += score_word(word)

def play_word(player, word):
  player_to_words[player].append(word)
  update_point_totals(player, word)

Thanks and sorry for the lack of appropriate information from the get-go!

1 Like

SCRABBLE is not a game that we can add a player to once it is underway. We play together with whomever we start the game.

I know that :laughing:!

But if it WERE such a game, how could the play_word() function be written to add both a new player and a new list as a key? I want to know so I can better learn to write code that affects dictionaries, rules of Scrabble be damned!

It is within the bounds of these rules that strengths and weaknesses are found, and we get to learn how to devise our architecture with governing constraints. Learn what you can from this, and then stretch it. Don’t expect it to stretch for you. Let’s focus on this exercise and succeed.

What is the containing object? What form do the contained objects take? In order to maintain homogeneity we need to conform to that.


Consider,

class Player:
  count = 0
  def __init__(self, name='player'):
    Player.count += 1
    self.id = self.count
    self.name = f"{name}{self.id}" if name=='player' else name
    self.words = []
    self.score = 0
  def __str__(self):
    return f"{self.id} {self.name} {self.words} {self.score}"