[Need your advice! ] scrabble_score


error message: Oops, try again. Did you create a function called scrabble_score? Your code threw a "global name 'scrabble_score' is not defined" error.

Does anyone can tell me what's the meaning of this error message? I wrote some similar code in different exercise but didn't get a message about "global name 'scrabble_score' is not defined" .
I am very confused 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):
    total = 0
    text = str(word)
    if text== "":
            return "You shouldn't type a empty string!"
        for letter in text.lower():
            if letter in score:
                total += score[letter]
            return total
                return False 
print scrabble_score("word")


When I tested your code, I haven't got the error you mentioned.

I got a syntax error.

if letter in score:
            total += score[letter]
        return total
            return False

Reason is simple..

We can't put anything between if and else block.

if condition:

It will give us an error.
You have put return statement (a new block of code between if and else)
You also don't need that else part, (for simplicity)
Remove it?

After you fix this, You still get wrong output, reason is wrong indentation,
As you've put return total inside the for loop, It will not able to add all point values of characters of word.


@greentreemee Thanks! It's working now!


For our purposes, assume word to be a string object.

There is no reason to have this and it is not expected. If an empty string is passed in, just return 0.

Again, we may assume the input to be a valid word so do not need to check if letters are present in the dictionary. They are, and the author of the course saw to that. Simply add the letter value to the total and leave it at that.

There is no expected return value except a number. The entire if statement may (and should) be removed, leaving only the total line.

That line should be at the very bottom of the function.


@mtf Hi, thank for your advice!
I think the whole editor is more simpler now ! Thanks!


As an aside, what the OP has the makings of is an error checking protocol. In a real world environment where complete strangers are running our code, error checking and error handling are mandatory. Here, not so much since we are pretty much the only user, and we have the code right in front of us.

On the bais that we want this function to be bullet proof, that is never respond to invalid inputs but raise an exception or squawk back a custom error message and ask for new inputs.

One such check could be to test the inputs are alphabetic letters. Python has a built in function for that purpose.

total = 0
if not word.isalpha(): print "Input Error: Non-letter characters detected."
    # code
return total

This may even pass the SCT since the return value is a number.

As for the empty string check, it is trivial but should still signal an error since it may indicate a problem somewhere else if you have a larger program calling this function. That being the case, it would suggest trapping this error at some point before the call, rather than have this function detect it.

Really, for that matter, even the above issue should be trapped before the call and this program left to perform its pure function... Look up letter scores.

Looking at a larger program, such as an infinite loop that takes inputs and does a quick query, the place to detect the errors is at the user input stage, and loop around that.

while True:
    print "Scrabble Score Checker is waiting..."
    while True:
        user_input = raw_input("Enter a word to check: [Quit = Esc]")
        if len(user_input) and user_input.isalpha():
    if user_input.lower() = 'esc':
    print scrabble_score(user_input)

Scrabble Score Checker is waiting...
Enter a word to check: [Quit = Esc] word
Scrabble Score Checker is waiting...
Enter a word to check: [Quit = Esc] esc

This passed lesson 9.


Note that esc is not a word, so we would never be expected to look it up. That is why I chose it, and not, 'quit' or 'exit'. These are words will might be looking up.


Your advice really benefited me a lot !


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