Scrabble_Score : What' s wrong?


#1

What's wrong with this code?

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 i in word:
        for m in score:
            if i == m:
                total = total + i
                score[m] = score[m] + 1 
            else:
                score[m] = score[m] + 1 
    return total
    
scrabble_score("pie")

Please tell me how to solve this problem.


#2

with respect to your code.

first thing to do is to convert your function's - input (parameter word) to lowercase as score card is defined for lowecase letters.

second..
You need to fix few things in if-block.
right now you're adding letter to total (which is an integer)
but you have to add score to total..

like this..

total = <access value from score using the key> + total

also you don't need else- block.

after debugging you can reduce one loop or so.
Your current code will work but it can optimized by reducing steps,


#3

I solved it. Thank you very much for your instructions !


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.