Function fails on scrabble_score("xenophobia"). It returns "23" when it should return "24"


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/3?curriculum_id=4f89dab3d788890003000096#


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


My function seems to fail on words with two or more of the same character: my for loop seems to only include one of every character in it's summation of the word. In this example, I believe the function is only accounting for one of the 'o' s in 'xenophobia'

Any thoughts on how to correct this?


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):
    scores = 0
    word = word.lower() 
    for key in score:
        if key in word:
            print score[key]
            scores += score[key]
    return scores
    
print scrabble_score("xenophobia")


#2

Why not just loop over word ?
we will also able to get rid of if- clause.


#3

You're right. I got it down to 6 lines.... no wait... Correction: 5 lines. Im a noob, so someone might probably be able to get it down to couple of lines less.


#4

You could print the letter with the value to know for sure. So:

print score[key], key

That should output something like:
8 x
1 e
1 n
1 o
3 p
4 h
1 o
3 b
1 i
1 a
24


#6

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