0

I have a dictionary that binds an integer to every character in the alphabet like so:

letter_values = {'a': 1, 'b': 3, 'c': 3} and so on

My program asks the user for an input, iterates over each character of the string and adds the respecting value from the dictionary to a variable called score, like so:

word = raw_input('Please enter your word: ')
score = 0
if len(word) == 0:
    return score
else:
    for letter in word:
        for letter in SCRABBLE_LETTER_VALUES.keys():
            char_value = SCRABBLE_LETTER_VALUES.values()
            score += char_value

For the line in which I assign a value to the variable char_value, I get the following error:

int() argument must be a string or a number, not 'list'

By that I assume that Python doesn't recognize that the value in the dict is an integer (?) If so, how can I add the integers of the dict to the variable?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Brian
  • 117
  • 1
  • 13
  • 1
    You're trying to add the whole of `.values()`, which is a list of *all values*, to an integer... Also, iterating over the dictionary like that makes absolutely no sense. Don't you just want `SCRABBLE_LETTER_VALUES[letter]`? – jonrsharpe Jun 28 '16 at 15:42
  • 1
    You are right with that, at the moment the code is not good. My problem is solved now, so I'm of to improve it! – Brian Jun 28 '16 at 16:05

1 Answers1

3

values() returns a list, not the specific value. Dictionary values can be accessed by square brackets, though. Try:

for letter in word:
    score += letter_values[letter]
Clay
  • 124
  • 6
  • 1
    Yep, Or `letter_values.get(letter,0)` if the OP is unsure that the element is present in the dictionary. [Why dict.get(key) instead of dict\[key\]?](http://stackoverflow.com/q/11041405) – Bhargav Rao Jun 28 '16 at 15:48