Reputation: 49
I've been working on some exercises, and have run into a problem with one of them.
The prompt:
"Scrabble is a board game, where a word is worth as many points as the sum of its letters. Write a function that calculates the point value of a word.
pointtable
, a dictionary containing the point values of all of the letters, and word
the actual word: a list of lettersI saw an example where we find the sum of values of dict so i tried to do it that way:
def scrabble_word(pointtable, word):
s=0
for k in word:
s += word[k]
return s
It was wrong, then I realized the word is a list, and changed it a bit:
def scrabble_word(pointtable, word):
s=0
for i in range(len(word)):
s += pointtable[i]
return s
It still wrong. Should I define the values first? Can you help me?
Upvotes: 0
Views: 487
Reputation: 530960
Assuming pointtable
is a dict like {'e': 1, 'q': 8, ...}
, then you want to use items from word
as the index into pointtable
.
def scrabble_word_score(pointtable, word):
score = 0
for letter in word:
score += pointtable[letter]
return score
Using more descriptive variable names can help you notice expressions that don't make sense.
Upvotes: 1