9/15 scrabble_score

#1

score = {“a”: 1, “c”: 3, “b”: 3, “e”: 1, “d”: 2, “g”: 2,
“f”: 4, “i”: 1, “h”: 4, “k”: 5, “j”: 8, “m”: 3,
“l”: 1, “o”: 1, “n”: 1, “q”: 10, “p”: 3, “s”: 1,
“r”: 1, “u”: 1, “t”: 1, “w”: 4, “v”: 4, “y”: 4,
“x”: 8, “z”: 10}
def scrabble_score(word):
word = str(word)
word_as_list = list(word)
word_as_dict = {k : 0 for k in word_as_list}
word_as_key_list = []
word_as_value_list = []
for key in word_as_dict.keys():
if key in score.keys():
word_as_key_list.append(key)
word_as_value_list = [score[x] for x in word_as_key_list]
personal_score = sum(word_as_value_list)
return personal_score

Your function fails on scrabble_score(“xenophobia”). It returns “23” when it should return “24”.

#2

Hi @fermently,

How many times does `"o"` occur in `“xenophobia”`?
How many times can `"o"` occur as a key in `word_as_dict`?
What is the value associated with the key `"o"` in `score`?
Along with the above, note that the message reported that the score was off by `1`.

#3

Hey, before I take your message into consideration, let me thank and compliment you. This is a clear suggestion. Exactly a type of nudge required.

#4

The above questions relate to the fact that any given key can appear only once in a dictionary.

Your `scrabble_score` function is more complicated than necessary. It isn’t necessary to create a new dictionary and several lists. A simpler solution could consist of the following …

• Initialize a `total` to `0`.
• Iterate through the characters in `word`, using each individual character as a key to get its point value from the `score` dictionary, then adding that point value to the `total`.
• After the loop, you can `return` the `total`.

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.