Scrable_score


#1

I found this code in another discusion side, it works but I do not understand " return sum([score[char] for char in word.lower()]) " Can someone please explain it to me?
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[char] for char in word.lower()])

print scrabble_score('pie')



Replace this line with your code.


#2

@arcsurfer86010

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):
    for char in word.lower():
        print("char: {0} value: {1}".format(char,score[char]))
    print("== Build a =list= Array")
    print("{0}".format([score[char] for char in word.lower()]))
    print("== Use sum() function on list-Value's ==")
    print("The sum is: {0}".format(
        sum([score[char] for char in word.lower()])))
    return sum([score[char] for char in word.lower()])
print scrabble_score('pie')

Reference
== the Book ==
From
https://docs.python.org/2/library/functions.html
we choose
https://docs.python.org/2/library/functions.html#sum
== discussions / opinions ==
http://stackoverflow.com/questions/18752171/python-building-creating-a-list-using-for-loop


#3

Easier example:
def scrabble_score(word):
word = word.lower()
points = 0
for i in range(len(word)):
points += score[word[i]]
return points


#4

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