6. scrabble_score what am i doing wrong?


#1

This 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}
         
def scrabble_score(word):
    x = word.lower()
    a = list(x)
    print a
    total = 0
    for key in score:
        if key in a:
            print key
            total += score[key]
            a.remove(key)
            print a, key, score[key], total
    return total
        
print scrabble_score('xenophobia')

I get this error:
"Oops, try again. Your function fails on scrabble_score("xenophobia"). It returns "23" when it should return "24"."

What is wrong with my code causing this? When I print it it shows this:

['x', 'e', 'n', 'o', 'p', 'h', 'o', 'b', 'i', 'a']
a
['x', 'e', 'n', 'o', 'p', 'h', 'o', 'b', 'i'] a 1 1
b
['x', 'e', 'n', 'o', 'p', 'h', 'o', 'i'] b 3 4
e
['x', 'n', 'o', 'p', 'h', 'o', 'i'] e 1 5
i
['x', 'n', 'o', 'p', 'h', 'o'] i 1 6
h
['x', 'n', 'o', 'p', 'o'] h 4 10
o
['x', 'n', 'p', 'o'] o 1 11
n
['x', 'p', 'o'] n 1 12
p
['x', 'o'] p 3 15
x
['o'] x 8 23
23

It leaves that o behind without counting it in the total


#2

Inside your function, the line:

if key in a:

should be:

for key in a:

this should make the function work though I think you've overcomplicated it. Here's the function that worked for me. It's a little simpler:

Hope this helps :slightly_smiling:


#3

tgraytrox,

Your score is diluted with excess code. I have posted my code. I hope it makes sense.

def scrabble_score(word):
total = 0
word = word.lower()
for c in word:
total += score[c]
return total

print scrabble_score('Helix')


#4

def scrabble_score(word):
word=word.lower()
total=0
for i in word:
total+=score[i]
return total


#5

one step cleaner (for the purists)....

def scrabble_score(word):
    total = 0
    for i in word.lower():
        total += score[i]
    return total

#6

Look here. You remove value from the score.


#7

How does this work? How is this working for uppercase letters?


#8

He uses the string.lower() method in the for loop.
This simply takes away the step of declaring: string = string.lower()