Scrabble Python project: Why isn't the code updating the scores?


#1

Hi,
I am wondering why the code is not updating the score of player after adding a new word just like so:

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 = {key:value for key, value in zip(letters, points)}
#print(letter_to_points)
letter_to_points[" "] = 0
#print(letter_to_points)

player_to_words = {"player1": ["BLUE", "TENNIS", "EXIT"], "wordNerd":["EARTH", "EYES", "MACHINE"], "Lexi Con": ["ERASER", "BELLY", "HUSKY"], "Prof Reader": ["ZAP", "COMA", "PERIOD"]}

def score_word(word):
  points_for_word = 0
  for letter in word.upper():
    points_for_word += letter_to_points.get(letter, 0)
  return points_for_word
    

def play_word(player, word):
  for players in player_to_words:
    if player == players:
      player_to_words[player].append(word)
  return    

player_to_points = {} 
for player, words in player_to_words.items():
  player_points = 0
  for word in words:
    player_points += score_word(word)
  player_to_points[player]= player_points

def how_many_points(player):
  return player_to_points.get(player, "Player doesn't exist.")

print(how_many_points("Lexi Con"))

print(player_to_points)


play_word("Lexi Con", "train")

print(how_many_points("Lexi Con"))


#2

lets see if player_to_words has updated:

play_word("Lexi Con", "train")
print(player_to_words)

it did, very good. But train is in all lowercase, if we look at letters see that they are all uppercase. String comparison is case sensitive.


#3

Hmm. But that’s why I put .upper() in the score_word function. I mean, that’s what I thought it would do.
My logic was that score_word counts points from already “uppered” words from the player_to_words dictionary.
Where do I get it wrong?

Also when I use the word “TRAIN” in play_word it doesn’t change the score neither.


#4

that would work if score_word actually gets called (functions only execute when called), which isn’t the case.

i haven’t done the actual project, i would have to have a look at the instructions but you didn’t include project url

to me, it would make sense that you call score_word within the play_word function


#5

yaaaaaaaay! I got this. :smiley: such a satisfaction!
I followed your advice, made a few trials and errors and there we go!

def play_word(player, word):
 for players in player_to_words:
   if player == players:
     player_to_words[player].append(word.upper())
 player_to_points[player] += score_word(word)
 return 

I added

  player_to_points[player] += score_word(word)

to the code and now it does what I expected from it to do. :slight_smile:


#6

especially because you figured it out yourself, i just gave a little nudge, and you solved it :slight_smile: That is more valuable in my opinion then me giving the answer plus explanation.

programming is about problem solving, which you did :slight_smile: