Scrabble_score


#1




My code doesn't return anything. I am trying to search through the dictionary to find the score that each letter has and then add it ont the list. This is the error message I receive:
Oops, try again. Your code looks a bit off--it threw a "string index out of range" error. Check the Hint if you need help!
please help, here is my 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,"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
    x=0
    for i in score:
        if word[x] in i:
            total=total+score[i]
        x=x+1
    return(total)


#2

lets add a function call so we can narrow down where the error occurs:

def scrabble_score(word):
    total=0
    x=0
    for i in score:
        if word[x] in i:
            total=total+score[i]
        x=x+1
    return(total)
print scrabble_score("Python")

wouldn't it make more sense to loop over word and then get the score associated with that letter?


#3

i thought i was looping over word. Can you please explain it to me more clerly please? Thanks


#4

you are looping over score:

for i in score:

which is the dictionary which contains all letters as key and there scrabble score as value.


#5

thanks that worked great


#6

issue resolved? If so, well done :slight_smile: You did most of the work


#8

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