9/15 Scrabble Score


#1
`def scrabble_score(word):
    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}
    word = raw_input("What is your word?")
    total = 0
    for letter in word.lower():
        total += score[letter]
    return total

Oops, try again. Your function fails on scrabble_score("pie"). It returns "0" when it should return "5". --> This is what it is saying. Pleas explain what is wrong.


#2

You have an unexpected raw_input() call, as word is given as a parameter.


#3

yeah I got it now. thank you.

But could you please explain how can I add a raw_input() so that the programm will know that this Input is the word it is looping?


#4

They have the test set up so that you cannot use a raw_input(), but in your own program, you could use raw_input() to receive a variable, then pass that variable as a parameter in the function.

guess = raw_input("What is your word?")

scrabble_score(guess)

#5

I understand, thank you very much for your help.


#6

You are very much welcome!

Just post again if you have troubles! :slight_smile:


#7

Hello. In case you are interested, that code will pass this part of the course but does not work as intended when done outside of this exercise. I was not able to figure out how to incorporate a raw_input into the code as I am still mostly a beginner (it made every score regardless of the word equal to 6 because the variable i used, "guess", itself was worth 6 points), but without it you still need to move "def scrabble_score(word):" to beneath the score dictionary. not necessary for the exercise but worth noting for real world applications of these skills.


#8

Variables shouldn't have quotes around them... This is probably causing your issue.


#9

My code for this exercise

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

#13