9.Scrabble - Why no need to convert to a string


#1

My code works fine, but I am just wondering why unlike the previous exercises that I do not need to convert the input into a string in order for the code to correctly run? i.e. I would have thought I needed new_word = '' as a line in the code and then reference that to figure out the cumulative score.

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()
    total = 0
    for a in word:
        total = total + score[a]
    print total
    return total

#2

well, word contains a string. And you have a dictionary where you can use the letters of the string stored in word to get the score belonging to this letter.

so in a way, the dictionary takes care of the conversion


#3

Which input needs to be converted to a string?

The parameter word will already refer to a string after the function is called.

With this loop header ...

for a in word:

... a will refer to a string.


#4

Ok yeah, I understand, I think I was overcomplicating things unnecessarily. Thanks


#5

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