Scrabble_score help


#1

My issue is that I don't know how to just reference a key from a dictionary. In my IF statement, I know that score[i] is referencing the values, which I don't want. But I'm not sure what to write to reference the keys.
Is there a way? I think my code would work otherwise, as it's saying if the element of z, is equal to a key of score{}, then add the value of that key to the total, over each iteration. Then return total.


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):
    word.lower();
    z=[];
    total=0;
    for k in range(0,len(word)):
        z.append(word[k]);
    
    for i in range(0,len(z)):
        if z[i]==score[i]:
            total=total+score[i];
    return total;

Thanks.


#2

When you try to access a dict by a key, it will return the value associated with it.

As such you only need to do the following,

Python: Scrabble Function

def scrabble_score(word):
    return sum(score[letter] for letter in word.lower())

That is it, all there is to it.

If you need any more help feel free to ask.

EDIT:

If you don't know what generators are yet then the following

Python: Scrabble No Generator

def scrabble_score(word):
    total = 0
    for letter in word:
        total += score[letter]
    return total

#3

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

scrabble_score("i love coding")


#4

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):
scrabble_score = 0
letters = list(word)
for letter in letters:
for key, value in score.items():
if key == letter.lower():
scrabble_score += value
return scrabble_score