Help about 'Scrabble' exercise in Python3 Dictionaries section

Hi Team,

Have been coding for scrabble exercise in Dictionary section (from section 9 to 14) and found that at the stage ‘14’, where it says the player wordNerd is winning by ‘1’ point.

As per my code, which I believe until that point is correct, says that ‘wordNerd’ and ‘Prof Reader’ ties down to 15 points each.

Here’s my sample of code:

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

#print(player_to_words)

#create a empty dictionary
player_to_points = {}

#print(player_to_words.values())

for player,words in player_to_words.items():
  #rint(player)
  player_points = 0
  for word in words:
    #print(word)
    player_points += score_word(word)
  #print(player, player_points)  
  player_to_points[player] = player_points
  #player_to_points.update({player: player_points})

#function score_word()
def score_word(word):
  point_total = 0
  for letter in word:
    temp = letter_to_points.pop(letter, 0)
    #print(temp, letter)
    point_total += temp
  return point_total 

print(player_to_points)  

which prints output as

{'player1': 12, 'wordNerd': 15, 'Lexi Con': 5, 'Prof Reader': 15}

Can someone help me to understand how does player ‘wordNerd’ is winning by 1 point pls? It should be a tie.

Thanks

Can you share the exercise url? Are you sure you copied the code correctly? I get an error:

you call functions (letter_to_points and score_word) which do not seem to exist in the code

Hi, Here’s the URL

https://www.codecademy.com/courses/learn-python-3/projects/scrabble

And the complete code:

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]

#combining lists to create a dictionary

letter_to_points = {key:value for key, value in zip(letters, points)}

#print(letter_to_points)

#add element to dictionary
letter_to_points[" "] = 0
#letter_to_points.update({" ": 0})

#function

def score_word(word):
  point_total = 0
  for letter in word:
    temp = letter_to_points.pop(letter, 0)
    #print(temp, letter)
    point_total += temp
  return point_total 
 
brownie_points = score_word("BROWNIE") 

#print(brownie_points)

#create a dictionary
player_to_words = {"player1":["BLUE", "TENNIS", "EXIT"], "wordNerd":["EARTH", "EYES", "MACHINE"], "Lexi Con":["ERASER", "BELLY", "HUSKY"], "Prof Reader":["ZAP", "COMA", "PERIOD"]}

#print(player_to_words)

#create a empty dictionary
player_to_points = {}

#print(player_to_words.values())

for player,words in player_to_words.items():
  #rint(player)
  player_points = 0
  for word in words:
    #print(word)
    player_points += score_word(word)
  #print(player, player_points)  
  player_to_points[player] = player_points
  #player_to_points.update({player: player_points})

print(player_to_points) 

Thanks.

you go wrong here:

temp = letter_to_points.pop(letter, 0)

using pop will remove items from the letter_to_points dictionary, which means once a letter is scored, it can’t be scored again. Which is not how scrabble works

i already found the points low, this explains it

Got it. Thanks.

Yes, with wordNerd on 32, he wins by ‘1’ margin. Cheers! Thanks.

1 Like