Scrabble exercise - dictionary lesson


#1

I am stuck with the following exercise.

https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-dictionaries/projects/scrabble

Here’s my code


letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
points = [1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 4, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10]

letter_to_points ={letter:point for letter,point in zip(letters,points)}
letter_to_points[' '] = 0
print(letter_to_points)

def score_word(word):
  point_total = 0
  for char in word:
    point_total += letter_to_points.get(char, 0)
    return point_total  

brownie_points = score_word('BROWNIE')
print(score_word('BROWNIE'))

For the word BROWNIE it returns me 3 instead 15 but i don’t get the reason.

Can you help me on that?

Thanks


#2

the letter b is worth 3 points, so it seems your function only awards points for the first letter. How could that be? How could you verify this is the bug you have? Any ideas?


#3

Thank you @stetim94.
I noticed it seemed to consider just the first letter.
I cannot understand wher the bug is as the for the loop should iterate throughout all char in word


#4

So you have determined that the loop does not behave as expected. What could be a next step? You could use this tool:

http://www.pythontutor.com/visualize.html#mode=edit

to see where things go wrong.


#5

My bad. indentation error.
Thank you for the tool!


#6

yes, and i could have just told you that you had in indent error. But i didn’t, on purpose. Because its not just about the error, its about how to debug. Such a tool can be very handy (or any other debug tool), or print statements can sometimes be useful too. Hopefully you learned something in this regard too :slight_smile: