Issue with practice lesson scrabble_score()


#1

My code works when I run it in python shell but it doesn't work in the shell for codeacademy. I'm not sure what I'm doing wrong. I'm suppose to define a function scrabble_score that takes a string word as input and returns the equivalent scrabble score for that word. Here is my 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}
word = raw_input('Please input a single word:')
lis = []

def scrabble_score(word):
x = 0
addPoints = 0

for w in word:
    lis.append(w.lower())
for l in range(len(lis)):
    if x == 0:
        x += 1
        addPoints += points(l)
    else:
        x += l
        addPoints += points(l)
return addPoints

def points(v):
for key in score.iterkeys():
if lis[v] == key:
return score[key]

scrabble_score(word)


#2

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):
m = word.lower()
point = 0
for x in m:
if x == str(x):
point += score[x]
return point


#3

Ok that makes sense. I like how your code is shorter. I suppose that comes with practice. One question, why did you put str() around x? If you want the two to be equal couldn't you say x == x? Why x == str(x)?


#4

Extra Study

Not sure that List Comprehensions have been covered yet, but this method is very nearly the same as above, but for the tiny difference...

def word_score(word):
    m = list(word.lower())    
    point = [score[x] for x in m]
    return sum(point)

Edit:

def word_score(word):
    return sum([score[x] for x in list(word.lower())])

#6

i don't understand the "if x == str(x):"


#7

Reason enough to seek other code to study. I don't understand it either.