Scrabble_score


#1



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


#2

here:

    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


#3

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


#4

can i see an updated version of your code


#8

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"

#9

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


#10

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