Scrabble_score


#1



Oops, try again. Does your scrabble_score function take exactly one argument (a string)? Your code threw a "string indices must be integers, not str" error.


Why will score.append(word[i]) not work? If I print it out it prints out the value.


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):
    score = []
    for i in word:
        score.append(word[i])
    return score


#2

Hi @cssrunner64366,

The problem is here ...

    score = []

... and here ...

    score.append(word[i])

First, initialize score to 0 instead of to an empty list, so that you can accumulate a numerical total.

Within the for loop, i represents each individual character in word. There, you need to add the value associated with that character to score instead of appending an item to a list.


#3

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):
word = word.lower()
score_count = 0
for n in word:
score_count+=score[n]
return score_count

This will work


#4

To avoid confusion, choose a different name for the function's local variable (score) because the dictionary's name is also score.

Initialize the local result variable to 0.

Loop through lowercase version of the word. For each letter in the word, add its corresponding dict value to the result i.e, score[letter].

HTH.


#5

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