Scrabble_score


#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".


I'm not sure why my code isn't working.


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):
    worl = word.lower()
    
    e = []
    
    for key in score:
        if key in worl:
            e.append(score[key])
    b = 0
    for a in e:
        b += a
     
    return b


#2

Your code doesn't check for duplicates, the letter 'o' is twice in "xenophobia" but only counted once.

This is because you iterate over the letters of the alphabet and checks if the letter is in the word. When looking for 'o' it doesn't matter if there is only one 'o' in the string or 9000 of them, the if-statement is executed. Then the score of the letter 'o' is added and python moves on to the next key.


#3

@mukadam.khalid1gmail,
You might want to change your

     for key in score:
        if key in worl:
            e.append(score[key])

into

    for key in worl:
            e.append(score[key])