Scrabble_score


#1

I couldnt figure our why it comes out 24...
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):
total=0
word.lower()
for x in word:
total=total+score["x"]
return total
Error:

Oops, try again. Your function fails on scrabble_score("pie"). It returns "24" when it should return "5".


#2

@lakait,

If you use

score["x"]

the associated Value of the property "x" will be used,
which is the number Value 8

As the =indentation= is very important
Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#3

I guess you are looping once. The idea is to compare each score[key] to each character in word, so if the character matches add the value of score[key] to total, at the end return total


#4

@ikechi,
even better
comparing each character in word to score[key]


#5
def scrabble_score(word):
    word = word.lower()
    total = 0
    for item in word:
       if item in score:
           total+=score[item]
    return total

This works for me, its almost indenticle to yours. except for one thing, see if you can notice it.


#6

Just for the purpose of sharing, this is my code:

def scrabble_score(word):
    word_score = 0
    for char in word.lower():
        for key in char:
            word_score += score[key]
    return word_score
print scrabble_score("hello")