PYTHON 3 Scrabble Help

The code look right:

# December 27 2020
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)}
letter_to_points[" "] = 0
print(letter_to_points)
def score_word(word):
  point_total = 0
  for letter in word:
    point_total += letter_to_points.pop(letter, 0)
  return(point_total)
     
brownie_points = score_word("BROWNIE")
print(brownie_points)

player_to_words = {
"player1": ["BLUE", "TENNIS", "EXIT"], #? 
"wordNerd": ["EARTH", "EYES", "MACHINE"], #? 
"Lexi Con": ["ERASER", "BELLY", "HUSKY"], #? 
"Prof Reader": ["ZAP", "COMA", "PERIOD"]}#? 
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
print(player_to_points)

# Put an input into the function so I can call it 

def play_word(player, word):
  for player, words in player_to_words.items():
    if player in player_to_words and word not in words:
      player_to_words[player].append(word)
    return player_to_words

Output:
{‘A’: 1, ‘B’: 3, ‘C’: 3, ‘D’: 2, ‘E’: 1, ‘F’: 4, ‘G’: 2, ‘H’: 4, ‘I’: 1, ‘J’: 8, ‘K’: 5, ‘L’: 1, ‘M’: 3, ‘N’: 4, ‘O’: 1, ‘P’: 3, ‘Q’: 10, ‘R’: 1, ‘S’: 1, ‘T’: 1, ‘U’: 1, ‘V’: 4, ‘W’: 4, ‘X’: 8, ‘Y’: 4, ‘Z’: 10, ’ ': 0}
15

The output looks right too until I get
{‘player1’: 12, ‘wordNerd’: 15, ‘Lexi Con’: 5, ‘Prof Reader’: 15}
Pretty sure the numbers are supposed to be way bigger

Have a look at the indentation in the play_word function. What order do you expect these lines to execute?

I’m also a little unsure about your use of .pop in your score_word function too. What is your intention in using .pop instead of an index here?

If you want to properly debug this consider adding some print statements at specific locations so you know what’s happening in your code. Alternatively a more advanced debugger might be warranted.

2 Likes

Technical aside

The zip() function is very friendly with a mix of iterables it would seem…

>>> list(zip('12345','abcde'))
[('1', 'a'), ('2', 'b'), ('3', 'c'), ('4', 'd'), ('5', 'e')]
>>> list(zip('abcde', [1, 3, 3, 2, 1]))
[('a', 1), ('b', 3), ('c', 3), ('d', 2), ('e', 1)]
>>> dict(zip('abcde', [1, 3, 3, 2, 1]))
{'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1}
>>> dict(zip('abcde', [1, 3, 3, 2, 1])).get('c')
3
>>> dict(zip('abcdefghijklmnopqrstuvwxyz',
	 [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])).get('z')
10
>>> 

Therein lies a way to cut down on typing.

1 Like

2 posts were split to a new topic: Python3 scrabble help