FAQ: Learn Python - Practice Makes Perfect - scrabble_score

This community-built FAQ covers the “scrabble_score” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise scrabble_score:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

1 Like

I used the following code to solve the scrabble exercise:

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

print(scrabble_score(“pizza”))

Which is shorter than the solution given. It makes me wonder now if my code is wrong or if I just found a shorter solution to the exercise.

4 Likes

I had the same solution, and now I’m also wondering.

1 Like

Hi!
I’m struggling with some of the basics on this one. Can someone explain each step of the solution code, I don’t quite get what each does.

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

print scrabble_score(“pizza”)

1 Like

The above uses the inner loop to confirm that letter is a character in score, and if not it gets ignored. There are other ways to do this but that would require explanation and may introduce new concepts not yet taught.

In a perfect world, we would not need to do this extra step on the assumption that non-alpha characters are not passed to the function. The shorter version posted above some time ago is an example of a function that makes this assumption. At its core it does what we want it to do, but will trip up if non-alpha characters are given as inputs. Either example is sufficient to pass this exercise since the test values contain only alpha.

3 Likes

Your explanation was really helpful thanks! I was also wondering this same thing.

My code is below and is really similar to the original post. I keep getting an error when I try to print at the end to test the code. When I remove the print statement it runs. Any help?

def scrabble_score(word):
  #this function will find the scrabble score for a given word using the provided dictionary
  word=word.lower
  word_score = 0 
  for char in word:
    word_score =word_score + score[char]
  return word_score

print (scrabble_score("pizza"))
1 Like

I suspect it runs properly but is not passing the SCT? Try removing the space after print. The SCT is not set up to recognize that as valid syntax.

We can perhaps blame me for using that syntax in so many of my examples, as I prefer to see print stand out.

2 Likes

Still no luck. This is the error I’m getting :

Traceback (most recent call last):
File “python”, line 15, in
File “python”, line 11, in scrabble_score TypeError: ‘builtin_function_or_method’ object is not iterable

1 Like

This says a lot about what’s going wrong. Functions/methods are indeed not iterable.

def f():
    ...

for v in f:  # f is a function, you can't iterate through this
    ...
1 Like

So it just won’t call the function in the print statement? It runs when I take the print statement out, and will let me move to the next lesson. I’m still kind of confused.

Isn’t that exactly what doesn’t happen? If you don’t call your function then it doesn’t run, does it?

You had an error message saying what went wrong. That’s what you ought to be following up.

1 Like

What you should be iterating through is a string. You have an error message saying you’re attempting to iterate through some function or method. Your error message also tells you where this happens, and since you only do iteration in one place you should be able to figure out where from that as well. From there you can can consider where you got that function or method from and/or ask yourself where your string should have come from since that’s what you should have been iterating through

If pure reasoning around your code and error message isn’t doing the trick, you can always insert prints in your code to write out what is going on. You’d probably want to write out the things that you rely on, or whatever information you want to know really.

Your error message provides you with something to investigate, that’s a very good thing. You’ll be able to locate and fix the problem by comparing what is happening (use prints to find out) to what should be happening (you should have already decided on this before you started writing code).

1 Like

This was my code for the problem, which gets the error "Your function fails on scrabble_score(“pie”). It returns “None” when it should return “5"” despite returning the correct total score:

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 = word.lower()
  total = 0
  
  for char in word:
    if char in score.keys():
      total += score[char]
  print total
      
scrabble_score("pie") # prints 5

What did I do wrong? I tested it out with print, and it prints the correct letters out, and also shows me the test values the site uses (“test”,“abc”,“pie”) which also return the correct scores.

The exercise expects a return value.

The score dictionary can be outside of the function, though it’s not doing any harm where it is. Only thing is it cannot be accessed from anywhere else so doesn’t serve a very universal purpose locked away inside a function.

2 Likes

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

print scrabble_score(“pizza”)

score is a lookup table so we do not need to iterate over it. Just poll the letter, as you have, here,

total = total + score(letter)

Thank you. I was also wondering why the second loop was necessary. Makes sense. Have to outsmart/out-dumb the user.

1 Like

I found the answer, but it says that’s wrong :angry:

def screabble_score(word):

total=0

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}

for letter in word:

for key in score:

  if letter.lower()==key:

    total+=score[key]

return total

print screabble_score(“DuNe”)

First thing to look for is typos.

Second thing to look for is complexity. Do we need a nested loop?

score is a dictionary. The keys are unique. That makes it an ideal look up table. Give it the key, and it returns the corresponding, or associated value. Using .get() we can set a default return value rather than raising a KeyError.

>>> def word_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, " ": 0
    }
    return sum(map(lambda x: score.get(x.lower(), 0), word))

>>> word_score('Banquet')
18
>>> word_score('@#$%')
0
>>> 
1 Like

can someone please explain what is the mistake i made with my code?
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 c in word:
if c in score:
total = score.values()
return scrabble_score

print scrabble_score(‘pie’)