9. scrabble_score / hints?


#1

Feel like I'm so so close...don't want to check answers, any hints to fix this?

ERROR MESSAGE:

Traceback (most recent call last):
  File "python", line 13, in <module>
  File "python", line 10, in scrabble_score
TypeError: unsupported operand type(s) for +=: 'int' and 'dict'

CODE:

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):
    total = 0
    for char in word:
        total += score
    return total

print scrabble_score("Fabrice")

#2

score is the dictionary, you will need to use dictionary + key to get the value, which is what you should add to total. I will leave it to you, to think about the required syntax, let me know if you need more help + include what you came up with


#3

Thank you @stetim94! I made the following correction and it WORKED (needed to add in the upper/lowercase correction too)

It's slowly starting to click...:smile:

CODE:

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

print scrabble_score("Fabrice")

#4

Good to hear it is starting to click, that is exactly what this exercises are about. Just something cool, you can do this:

for char in word.lower():

this will make word lower, and then loop over its characters. It is a bit neater, but good job getting it to work :slight_smile:


#5

Yes that is more elegant. Thank you!


#6

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