Scrabble project: What do I do to add words into dictionary

I’m doing this project in Python and here’s my full code up to the occurrence of the problem:

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 = {letter:point for letter, point in zip(letters, points)}
letter_to_points[" "] = 0

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

brownie_points = score_word("BROWNIE")
#print(brownie_points)

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
#print(player_to_points)

#play_word()
def play_word(player, word):
  for player, words in player_to_words.items():
    if player in player_to_words and word not in words:
     player_to_points[player] = word
    return player_to_words
play_word("Lexi Con", "PARABLE")

print(player_to_words)

Here’s the output:

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

You can see that the word PARABLE isn’t included in the dictionary player_to_words. I also looked up and tried the code in the replies, but the desired outcome would not yield. Need help. Thanks.

return does literally what it says, returning/handing back data to the function call/caller. Which signals that a function is done executing.

looking at your return player_to_words, your loop only makes a single iteration, given within the first iteration the return keyword is reached.

which means, your function only works for adding words to the first player.

with a debugger like this one:

Python Tutor - Visualize Python, Java, JavaScript, C, C++, Ruby code execution

which allows you to step through your code, you can find this kind of errors, and “visualize” the execution process.

3 Likes

I’ve tried the visualization, but I still don’t quite understand how things work. Maybe can you elaborate a bit more?
Meanwhile I’ve tried to change my code so it works in my favor, but it resulted in an error.
The code:

def play_word(player, word):
  if player in player_to_words and word not in words:
    player_to_words[player] = player_to_words.get[player].append(word)
  return player_to_words

play_word("Lexi Con", "PARABLE")
print(player_to_words)

The error:

Traceback (most recent call last):
  File "scrabble.py", line 33, in <module>
    play_word("Lexi Con", "PARABLE")
  File "scrabble.py", line 30, in play_word
    player_to_words[player] = player_to_words.get[player].append(word)
TypeError: 'builtin_function_or_method' object is not subscriptable

I wonder if someone can explain this error to me? Thank you.

Why did you remove the loop? We need to loop over the different players.

.get() is a method, calling a method requires parentheses. Square brackets are for accessing strings and array by index or dictionary by key.

1 Like

I actually don’t know what the loop does, so I removed it.
I’ll fix this tomorrow. Thank you for your feedback.

Maybe insert some print() statement to see what the loop does?

1 Like
def play_word(player, word):
  for player, words in player_to_words.items():
    if player in player_to_words and word not in words:
      player_to_words[player].append(word)
  return player_to_words

I fixed the indentation on return line, and the word PARABLE finally appears on the dictionary, just not in the way I expected:

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

How do I change the code so that PARABLE only appears in value of Lexi Con?

Sorry, been a while since I done this exercise

Could you provide the exercise url? I would like to see the task/requirements

if your purpose is to add a word to a single player, you don’t need a loop. Sorry for the confusion

but then this condition:

 and word not in words:

has to be different.

you can get the words of a player by doing:

player_to_words[player]
1 Like

Here is the url: Scrabble

I assume the task at hand, is this one:

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

one of the extra challenges.

Then we don’t need a loop. You are/where really close here:

def play_word(player, word):
  for player, words in player_to_words.items():
    if player in player_to_words and word not in words:
      player_to_words[player].append(word)
  return player_to_words

this is pretty much it, but then without the loop. Given we only need to add a word to one player.

you verify the user exists and word doesn’t already exists. Then add the word correctly to the played words

1 Like

Thank you so much! I changed the code to:

def play_word(player, word):
  if player in player_to_words and word not in player_to_words[player]:
    player_to_words[player].append(word)
  return player_to_words

play_word("Lexi Con", "PARABLE")
print(player_to_words)

and now it prints the exact output I wanted:

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

Guess I can celebrate New Year without the worry that there are code stuck from last year (it’s unlucky in our culture to leave something undone in the past year)

2 Likes