Score_word Function Does Nothing In My Code

When I tested my score_word() function on the word “BROWNIE” it returned 0. Why? It’s not that the code somehow didn’t find the letters in letters_to_points, because when I changed my get from .get(letter, 0) to .get(letter, "Don't tell me these letters aren't in my dictionary?!") my code still returned 0, rather than the aforementioned string. Here is my 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]
letters_to_points = {key:value for key, value in zip(letters, points)}
letters_to_points[" "] = 0
def score_word(word):
  point_total = 0
  for letter in word:
    letters_to_points.get(letter, 0)
  return point_total
brownie_points = score_word("BROWNIE")

Any answers are very much appreciated!

you never increase point_total (which keeps track of the total points for a word).

you only get the points for the letters, that is it

1 Like

I can’t believe I missed that! Thanks!

you can use defaultdict to make misses have a score of 0
key-value pairs, such as those you have from zip, can be given to dict (or defaultdict), there is no need for a dict comprehension
your loop is map and sum, you could use those instead