Scrabble

Hello,
I have a problem in my code,
wordNerd should be winning by 1 point but in my code he gets the same points as Profreader.
Wish you could help thanks :slight_smile:

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

def score_word(word):
  point_total = 0
  for key,value in letter_to_points.items():
    if key in word:
      point_total += value
    else:
      point_total += 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)

Have a little look into what happens with words that contain repeated letters (a word like “ODD” for example).

As an aside there’s no requirement for an if statement to contain an else clause if you wanted to simplify things a touch.

1 Like