How do i get the value of the keys in dictionary?


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/3?curriculum_id=4f89dab3d788890003000096#

Oops, try again. Did you create a function called scrabble_score? Your code threw a "global name 'key' is not defined" error.


Im trying to get the value of the keys but it threw an error that key is not defined. How do i index the dictionary to get the key?


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):
    total = 0
    for score[key] in str(word):
        if score[key] in word:
            total += score[value]
    return total


#2

Please see my comments on your 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}

def scrabble_score(word):
    total = 0
    for score[key] in str(word): # Should be: for key in word.lower():
        if score[key] in word: # Should be if key in score:
            total += score[value] # Should be total += score[key]
    return total

If you have any questions on how or why this works, please just ask.


#3

ahah i was so close. Would it be the same if i do score[key] instead of just key? for score[key] in str(word)
why did you do word.lower instead of just word?

total += score[key]

is score[key] getting the value of the key or just key?
thank you.
I need a refresher on dictionary.


#4

Let's go through this code one line at a time...

def scrabble_score(word):

This line is kind of self explanatory. You are defining a function called scrabble_score with the parameter of word.

    total = 0

Another self explanatory line, we are defining a variable called total and setting it equal to 0.

    for key in word.lower():

This line is looping through each key (letter) in word. We have to use key, because if used score[key] the program would think you are looking for the value of key from score. However, there is no key in score. This means that you would receive a syntax error. This is not what we want. You wouldn't even have to use key necessarily, you could call it anything you want so long as what you call has neither been defined nor is a built-in function, identifier, or variable. Also, we have to use word.lower() because the instructions specifically state: Your function should work even if the letters you get are uppercase, lowercase, or a mix.

        if key in score:

This line tells the program to run the code inside the if statement, if key is in word. We have to use key here again, because key is what we used in our for loop and that's the item we want to check the value of in score.

            total += score[key]

This line is telling the program to add the score value for each key (remember that key represents the individual letters in word) to total.

    return total

Lastly, this line is also self explanatory (but a necessity, nevertheless). It just returns our final value for total.


#5

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