0

Hi i am working on Codecademy exercise 9/15. The goal is to create a scrabble function that takes a string as input then returns a score for that word.

They give you a dictionary to start off with and this is what I have so far after searching for "how to loop through a dictionary and add values" on google.

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}




1) total = 0 # takes your input word value and saves it to total 


2) def scrabble_score(x):       # defining function name 
3)     for i in score.values(): # loops through score           
4)         total += i           # sums up your input key value
5)     return i 

this code keeps throwing a local variable total error.

does this mean that the total variable does not work with the scrabble_score(x) function?

P Ramos
  • 31
  • 1
  • 2
  • 5
  • 1
    You also have a different problem. Note that you never use the variable `x` in your function, meaning whatever result is returned does not depend on the input word. You would also probably do better declaring `total` inside of the function, so that you can calculate a different total each time the function is called. – Platinum Azure Aug 26 '15 at 19:28
  • You're also returning `i` which means you're just returning the last score. – Morgan Thrapp Aug 26 '15 at 19:29

3 Answers3

3

You need to put total = 0 inside the function, and you need to loop through the input word, then add each letter's score from the dictionary. You also need to return total, not i.

SCORES = {"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
    for letter in word:
        total += SCORES[letter]
    return total

Here's another way to do it, to keep in mind for the future:

def scrabble_score(word):
    return sum(SCORES[letter] for letter in word)
Cyphase
  • 11,502
  • 2
  • 31
  • 32
0

You have total += i but there is no variable named total in scope at this point. Consider initializing it with total=0 before the loop. Or, declare total as a global variable.

Also, your loop does not appear to be considering the value of x, so all it's going to do is calculate the total of all possible scores.

And then return an index instead of the total.

larsks
  • 277,717
  • 41
  • 399
  • 399
0

When you use a dictionary, you don't need to loop through it. the way to access a dictionary value is very simple. For example in the dictionary you have:

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}

if you wanted to get the value for 'a' all you would have to do is: score['a'] this will return the value that is set for the key 'a', you would get 1. if you wanted to use variables you could do something like this:

test='b'
total=score[test]
print(total)

you would get:

3

all you have to do is loop through the String that you have and call each letter and add them to the total. make sure you set the total to 0 before you start too or else you would get errors. once you are done looping through each letter in the word return the total.

Buzz
  • 1,877
  • 21
  • 25