# Scrabble Code - Works in actual Python, not here. Why not?

#1

Can anyone see what might be returning the wrong value here, but not in Python itself, or in other Python simulators? 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}

lst = []
def scrabble_score(word):

num = 0
word1 = word.lower()
for let in word1:
for k in score:
if let == k:
num = score[k]
lst.append(num)
else:
continue
return sum(lst)``````

"pie" , for example, returns 16 here, but 5 in Python.

#2

Try to execute your function multiple times. Run this code:

``````print scrabble_score("pie")
print scrabble_score("pie")``````

It should return:

``````5
10``````

Strange, right?

This happens because you defined `lst` as a global variable, that is why only the first execution will give a correct result. Move definition of this variable to the body of the function:

``````def scrabble_score(word):
lst = []
num = 0
word1 = word.lower()
for let in word1:
for k in score:
if let == k:
num = score[k]
lst.append(num)
else:
continue
return sum(lst)``````

#3

You know, I originally had it as a local variable in the function, and I thought it wasn't returning properly. Maybe I simply mistyped it the first time. Thanks for your help!

#4

You're very welcome

A small hint from me -> marking your own post as a solution might be considered as a bad behaviour.

#5

Ha! Wrong button (I'm not quite so egotistical, I hope, just careless! )