The solution of scrabble_score problem

`def scrabble_score(word):
word = word.lower()
total = 0
for letter in word:
for leter in score:
if letter == leter:
total = total + score[leter]
return total

I can’t understand the solution.
why did we make two for loops?
why did we write score not word in second for loop ?
and what’s the meaning of letter == letter


First question… Where did you find this solution?

This function only needs a single look up loop (read only) and no other.

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

score is the name of the look-up table (dictionary). The letter is the key, and the value is the score.


on code academy…I clicked on solution
this function “scrabble_score” will work with score dictionary only right
if i make another dictionary with different name it won’t work .
why i can’t use “word” instead of “score”


I have tried it before many times by using only one for loop but with “word” instead of “score” but It didn’t work too.


We do not need to pass the score dictionary to the function, just a word (a string).

 print scrabble_score('surprise')

Inside the function, word will equal 'surprise', and the loop will iterate over the letters.

That would explain where the code came from, and why there are so many questions in the forum about it.

For a beginner course, the author’s solution is very convaluted and not well thought out. I’m surprised that CC published it, unless their intent was to confuse learners and raise lots of questions. Lesson there is to expect solutions to be mediocre, some we can learn from, others we can take as examples of what not to do. Either way we learn, so there is that take away.

Right, when given the inputs that are expected (string).


Extra Study

It won’t come up until later in the course, but when it does, come back to this thread for an example. Don’t spend any time on this, right now.

>>> 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):
    First we filter out numbers and None.
     - A string, list of strings, or a dictionary will be passed.
     Next we check for non-alpha characters.
     - Only a-z and A-Z will pass
     The valid return is the sum of the collective letter scores.
        word = ''.join(word)
    except TypeError:
        print ("Input Error: Numbers and None prohibited.")
    if word.isalpha():
        return sum([score[x] for x in word.lower()])
    print ("Input Error: Non-alpha characters prohibited.")

>>> scrabble_score(1)
Input Error: Numbers and None prohibited.
>>> scrabble_score([1,2,3])
Input Error: Numbers and None prohibited.
>>> scrabble_score(None)
Input Error: Numbers and None prohibited.
>>> scrabble_score('sun rise')
Input Error: Non-alpha characters prohibited.
>>> scrabble_score(['a','b','c'])
>>> scrabble_score(['A','B','C'])
>>> scrabble_score('surprise')
>>> scrabble_score({'one':3,'two':6,'three':8})

Q. Why allow a dictionary?
A. No reason, at present. It has a .join method so we let it pass. What’s more the method gives a string, which is valid input. Notice that the given string is a concatenation of the keys.

The net aim of the above error checking is to preserve and exploit robustness. We can send more than one word and get a score which means we’re half the way there to solve intersecting words, in one step. All that remains is discounting intersection points. A tile cannot be counted twice.

As the dictionary goes, that is a role that would be left for future design possibilities. No harm, no foul. The option should be left open.

Included in the unused, but usable category, tuples and sets. They, too have a .join method.

>>> scrabble_score(('A','B','C'))
>>> scrabble_score({'A','B','C'})


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