trang nguyen
trang nguyen

Reputation: 65

Using a variable as a dictionary key, is it possible?

I have a dictionary with a corresponding value to each letter. I need to go through a string and calculate the sum of all letters' value.

    SCRABBLE_LETTER_VALUES = {
'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10
} 
word = 'maths'
for letter in word:
    score += SCRABBLE_LETTER_VALUES[letter]

The output score should be a sum of whatever value corresponding with the letters in word. But I can't get Python understand I'm calling a key by a variable type string.

What is your solution for this? Any help is appreciated!

Upvotes: 0

Views: 145

Answers (6)

Aaditya Ura
Aaditya Ura

Reputation: 12669

You can try map approach :

print(sum(map(lambda x:SCRABBLE_LETTER_VALUES[x],word)))

output:

10

Upvotes: 2

RewordedAnswers
RewordedAnswers

Reputation: 29

You need to set score to zero before you use it

Upvotes: 0

Anton vBR
Anton vBR

Reputation: 18906

The built in function sum() in python accepts comprehension. Your code can be simplified to:

SCRABBLE_LETTER_VALUES = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 
                     'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 
                     'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 
                     'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 
                     'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10}

word = 'maths'
score = sum(SCRABBLE_LETTER_VALUES[i] for i in word.lower()) # use lower for small letters

print(score)

And you get:

10

Upvotes: 2

Patrick
Patrick

Reputation: 17

Also, you might want to make the 'word' variable more dynamic by turning it into an input value:

SCRABBLE_LETTER_VALUES = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 
                     'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 
                     'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 
                     'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 
                     'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10}

score = 0
#Make sure to force a lowercase
word = str(input("What word would you like to enter?").lower())
for letter in word:
     score += SCRABBLE_LETTER_VALUES[letter]
print(score)

Upvotes: -1

Jai
Jai

Reputation: 3310

  • You have forgotten to initialize score variable to 0.

  • Code:

    SCRABBLE_LETTER_VALUES = {'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 
                             'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 
                             'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 
                             'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 
                             'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10} 
    
    score = 0    #  ============ > the line to be added
    word = 'maths'
    for letter in word:
        score += SCRABBLE_LETTER_VALUES[letter]
    
    print(score)
    
  • Output:
    10

  • If you do not initialize a variable you will get the following error: NameError: name 'variable' is not defined... Which means you are using the variable before initializing it and it is wrong

Upvotes: 3

Ollie
Ollie

Reputation: 1702

Add the line score = 0 before your for-loop.

Upvotes: 1

Related Questions