Exercise 9 scrabble_score

The error is the following:
Traceback (most recent call last):
File "python", line 19, in
File "python", line 16, in scrabble_score
KeyError: '1'

My code works great, I have decided to improve it by not letting a user to input numbers, since then I have some errors. (I have added elif expression and the code crashed)

If someone can show me the direction to resolve that issue that would be awesome!

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):
    if len(word)<=0:
        print "Enter a valid word"
    elif word.isalpha()==True:
        print "Enter a word without numbers"
    for letter in word:
    return total_score
print scrabble_score("1122")



    elif word.isalpha()==True:
        print "Enter a word without numbers"

if word contains numbers, isalpha will return False, so you should if word.isalpha() equals false

then, once we have an error, we need the function to end. A return keyword can do this, so instead of printing the error message string, return it


Thank you. After the changes, still get the same error.
maybe I am missing something else?


can i see an updated version of your code


The problem was that your code was printing the string message twice; inside the function and when calling the function. In this case, you should return the string message and then print it outside the function.

    if len(word)<=0:
        return "Enter a valid word"
    elif word.isalpha()==False:
        return "Enter a word without numbers"


i just tried to run your codes and it worked for me, but i am n not yet to figure out your concept


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