9/15 Scramble Score Answer


#1

Here is what I did:

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

#2

You can also try this

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):
    return sum(score[i] for i in word.lower().replace(" ",""))
    
print scrabble_score("code academy")
print scrabble_score("codeacademy")

#3

@stan1023,

And how would you call the function in such a way, that you get a displayed result ??


#4

@rydan that works too
@leonhard_wettengmx_n what you mean?


#5

To get a displayed result you would just
print (newscore) rather than return (newscore)


#6

@marmaa,
What i meant was

print scrabble_score("scrableword")

#7

You can also try this:

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}

word = ""

def scrabble_score(word):
suma = 0

palabra = str(word.lower())

for i in palabra:


    suma = suma + score[i]



return suma

#8

this is the same solution @stan1023 suggested originally


#9

Can you explain why is the .replace(" ","") necessary? Just out of interest :smile:


#10

that statement gets rid of all spaces in the given string. because in the dictionary of letters there is no score for spaces.


#11

Ah ok! Thanks now I understand!