Scrable_score


#1

plz help!!!

my code is :

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(text):
    s = 0
    for i in text:
        if str(i).lower() in score:
            s += score[i]
    return s
print scrabble_score("syhOd")

it works fine when i enter a lowercase string, but when i try to enter a mix string as parameter it shows this error:

Traceback (most recent call last):
File "python", line 12, in
File "python", line 10, in scrabble_score
KeyError: 'O'

am really stuck here..help plz..thx


#3

Hint: str(i).lower() will not change the variable i to lowercase unless you reassign the value of str(i).lower() to i.

So, when you encounter any character i as say upper case "O". While you are indeed converting the value to lowercase, but i remains unchanged. So, what you do on line:

s += score[i]

is same as:

s += score["O"]

and hence the error. Can you now fix it?

@stetim94 OP has already used if str(i).lower() in score so the line s += score[i] will never execute for any character not in score dictionary.