Scrabble (Dictionaries)

Here is the link to the exercise:
https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-dictionaries/projects/scrabble

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 = {key: value for key, value 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.pop(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)

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 am working on the “Further Practice” portion now in where I want to add a new word value to a player key.

My function seems to work but for some reason I don’t understand it adds my word value to the wrong player key. for example this is my output.

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

As you can see I called (“Lexi Con”, “ADD”) but my function added to “player1”. I did the reverse in calling “player1” and it actually added to “Lexi Con”. I don’t understand why. Help please?

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

Is this return inside the loop for a reason?

1 Like

If I remember correctly I tried it outside the function and it gave me the same result.

I just tried it again and it yielded the desired output. I know it might seem ridiculous to believe me, but I did test my function with the return outside the function.

Thank you

1 Like