9.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=word.lower()
    sum=0
    for key in score:
        if key in word:
            sum = sum + score[key]
    return sum
word='Bbb'
print scrabble_score(word)

Oops, try again. Your function fails on scrabble_score("xenophobia"). It returns "23" when it should return "24".

I can't understand this error


#2

this error make sense, you loop over the keys in score, but xenophobia has two o, but you only get one o, which is why you are one point short.

I would simply loop over the word, and then access the required value from the dictionary to add to total


#3

Oh,I see.Thank you for your reply


#4

Did you resolve the issue?


#5

yes

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=word.lower()
sum=0
for letter in word:
sum = sum + score[letter]

return sum

word='Bbb'
print scrabble_score(word)


#6

well done!