Scrabble_score issue


#1

Cannot figure out why this is not working:

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:
    
        total += total + score[letter]
    
    return total

Throws the error:

Oops, try again. Your function fails on scrabble_score("pie"). It returns "15" when it should return "5".


#2

Hi @tesselating ,

You have ...

total += total + score[letter]

The += operator adds the value of the entire expression on the right side of the operator to the value of the variable to the left, including what is already in total.

You have two choices for fixing this. Either use = instead of +=, or remove total + from the expression on the right side.


#3

Ahh, yeah, I see what I have done. Very clear explanation, thanks!


#4

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