Scrabble_score --> similar solutions work, why doesn't mine?


#1



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


I keep getting the following error message:

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


I found very similar solutions to mine in the forum, and I understand why they work. But I do not understand why mine does not, so an explanation would be much appreciated. :slight_smile:


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 = word.lower()
    for i in word:
        if i in score:
            total += total + score[i]
    return total

Thanks for your help!!


#2

here:

total += total + score[i]

+= is a short-hand, so if we write it out full:

total = total + total + score[i]

that is adding another total, which will create a higher score

either use the shorthand or @aquaphoenix17 suggestion but don't combine them


#3

Here:

    for i in word:
        if i in score:
            total += total + score[i]

As @stetim94 said, total += total + score[i] is the same as total = total + total + score[i].

However, this is not what we want. To fix this, either remove the total + or change the += to an =.


#4

you guys are sooooper quick! my compliments and thanks! :slight_smile:


#6