Scrabble_score returns 0


#1



I really thought I was getting this right..
It returns 0 instead of 5
Any thoughts?
Thanks a lot!


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()
    total = 0
    for letter in word: 
        for key in score:
            if letter == key:
                total== total + score[key]
                return total

Your function fails on scrabble_score("pie"). It returns "0" when it should return "5".


#2

Should not have any indent on it beyond the immediate function block. Right now it is buried in an if statement.

Since score is a dictionary, we can look up keys directly without iterating.

for letter in word:
    total += score[letter]

The instructions don't go into edge cases such as non-alpha inputs but we can modify the code to tighten it...

if not word.isalpha(): raise ValueError

#3

Thanks a lot!
I changed it to score[letter] instead of score[key]/ plus the indentation for return , and it worked immediately!


#4

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