[SPOILER - SOLUTION] Scrabble Project: Feedback wanted

SPOILERS - SOLUTION AHEAD



Hi.

I’m new and like feedback. What could I have done better?

Here’s how I did the Scrabble Project:

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 = dict(zip(letters, points))
letter_to_points[""] = 0

def score_word(word): return sum([letter_to_points.get(char.upper(), 0) for char in word])

player_to_words = {
  "player1": ["blue", "tennis", "exit"], 
  "wordNerd": ["earth", "eyes", "machine"], 
  "Lexi Con": ["eraser", "belly", "husky"],
  "Prof Reader": ["zap", "coma", "period"]
  }

player_to_points = {player: sum([score_word(word) for word in player_to_words[player]]) for player, words in player_to_words.items()}

print(player_to_points)

Hi there,

Your code looks good; however, on line 7:

def score_word(word): return sum([letter_to_points.get(char.upper(), 0) for char in word])

The pep 8 style guide recommends against doing this:

Compound statements (multiple statements on the same line) are generally discouraged

Also on line 7 again, consider using setdefault() instead of get():

letter_to_points.setdefault(char.upper(), 0)

Python dictionary method setdefault() is similar to get(), but will set dict[key]=default if key is not already in dict. If you take that recommendation, you can get rid of line 5:

letter_to_points[""] = 0

If you use setdefault() you can set a default key value of zero for any character not in the dictionary already (such as a blank tile) . So that line (in my opinion) is not needed.

Another thing you can do to improve your code is to add some comments! Other than that, you did a great job! Keep up the good work!