9/15 How can I iterate through values of the dictionary?


#1

I'm ■■■■■■■■ so I can't think of how to do this...

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 l in word:
        for l in score:
            print total == total + score[l]

scrabble_score("pie")

The result is False False False(...) Which seems to me like it iterates through keys, and not values. I tried messing with it in any way possible, but i don't know how to iterate through values without passing it in, like 'a', 'b', 'c' etc...
I could try doing something diferent altogether, but the problem still stays, and I wanted to do it like the hint says.


#2

You do not want to iterate over the dict, the power of the dict is that you pass it a value and it returns a value.

a_dict = {'a': 1}

a_dict['a']
# OUTPUT: 1

So your code should do this,

for letter in word:
    total += a_dict[letter]

Easy enough?


#3

Are you trying to assign values here or making a comparison?

If you're assigning, the syntax should be:

 total = total + score[l]
print total

#4

Thanks! It's really simple solution and it works fine.


#5

@scrubble looks like you're on the right path - here are a few edits message me if you have questions

def scrabble_score(word):
    total = 0
    for l in word.lower(): #note dont forget to use .lower() for lowercase letters
        total = total + score[l] 
    return total