9. scrabble_score- stuck


#1




Really not sure what's wrong,,, is the logic at least ok? frustrated:(

Oops, try again. Does your scrabble_score function take exactly one argument (a string)? Your code threw a "'int' object is not iterable" error.


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):
    word.lower()
    for letter in word:
        for v in score:
            while letter == score[v]:
                return score[v]
        return sum(score[v])


#2

What you wanted to do is check the letters of word with the key of the dictionary score. Instead, you checked the letter with the value of the dictionary. Correct code should be:
if letter == v

Also, once your while loop's condition is true, your function will return the value of the matched key and end there, not proceeding to the last statement i.e.

I have modified your code as follows. Although I am not sure whether it would work or not.

The solution that I have used and is working, is given below. See if you can make sense out of it.

Working Code

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

#3

why don't u need to declare score?


#4

thanks it works for me.


#5

Use a simpler code don' t nest it with too many for's and while's.
Sorry about the indentation but this worked for me. Use the raw_input to check if it works and enter letters only.
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}
h = raw_input("Enter a string of letters:")
def scrabble_score(word):
x = word.lower()
y = list(x)
total = 0

for a in y:
    total = total + score[a]    
return total

print scrabble_score(h)


#6

Score is already declared as the dictionary at the beginning of the code. meason simply didn't copy and paste the whole thing because it's long.


#7

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