8/9 scrabble_score


#1

I got an error ‘int’ object is not subscriptable. Also my code is much simplier than the answer so I was wondering if it’s the right way to do it.

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

#2

here:

score[char]

what are you doing/attempting?

score is the total score of data type integer, it can’t be accessed with square brackets


#3

Hi. I’m trying to add the point values of individual letters in ‘word’.


#4

close, you are trying to add point values of individual letters in ‘word’ to score

Seems i missed something, score (which keeps track of the score) and the dictionary (also named score) have the same name, how is python to know which is which?


#5

I can see my mistake now. I named the variable to be the same name as the dictionary so it doesn’t work. I named the variable to be total and it works now. Thank you!


#6

i missed that you gave the variables the same name, i simple thought here:

score = score + score[char]

that score is an integer, which can’t be accessed by key or index like you attempt with the square brackets

i came to this conclusion, given this was the error message thrown at us by the python interpreter.

in a sense, what i thought was true. The local variable (score = 0) is used over the global variable score (dictionary). which explains the error message


#7

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