Scrabble_score


#1

Help me, please! Why is this code failing?

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):
    w=raw_input('Enter a word:')
    w=w.lower()
    w=w.strip()
    num = list("0123456789")
    for x in w:
        if x in num:
            w=w.replace(x,'')
    sum=0
    for i in w:
        if i in score:
            sum+=score[i]
print 'Your score is:{}'.format(sum)

Oops, try again.
Your function fails on scrabble_score("pie"). It returns "0" when it should return "5".


#2

None of the above is needed by the program. We are passing a word into the function, not getting user input inside the function.

Your code is much too complicated. All we need is a simple lookup iteration.

    total = 0
    for x in word.lower():
        total += score[x]
    return total

print scrabble_score("xylophone")    # 24

In the above, x is a letter, a string. When used in score[x] it looks like, score["x"] which has associated value, 8

score['x'] => 8
score['y'] => 4
score['l'] => 1
score['o'] => 1
score['p'] => 3
score['h'] => 4
score['o'] => 1
score['n'] => 1
score['e'] => 1
================
             24

#3

Thank you, I understood!