Scrabble


#1

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

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

This is my code. I have figured out the correct coding but I'm wondering what the difference is and why wont the above one work? Thanks!

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


#2

Can't modify strings. And there's no reason to either, you need the lower-case version but you don't also need to change the original, that step should be left out.


#3

So the bottom one would be correct? And the top one is wrong because I cannot modify a string and shouldn't change the original word?


#4

i is an item in a look-up loop. It is not an index, nor can it be used as an index in a list. The indices must be integers. This line should be raising an exception that will show in the console, in red.

But if it doesn't raise an error, then the next line makes perfect sense. score is a dictionary, and dynamic variables are acceptible keys, so i is a valid key. The place in both examples to apply .lower() would be in the for parameters.

for i in word.lower():

#7

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