repl.it
@swill123/

ScrabbleScores

Python

No description

fork
loading
Files
  • main.py
  • scrabbleData.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# This program uses a dictionary and lists to sum up the scores for each player in a game of Scrabble

import re

# Create a list of records
records = []

# Create a dictionary of players
players = {}

# Open and clean up the file
# (I had to cheat and fix the typo in line 22 by hand)
# Split each record (line) into its own list and append that list to records (the list of lists)
with open('scrabbleData.txt','r') as f:
  for line in f:
    clean = re.sub(r"[\(\):]|pts|pm|,|\n", r"",line)
    clean = re.split(r"\s+|\n+", clean)
    records.append(clean)

# Populate (?) the dictionary with player names (the keys) from the records
# Create a list for scores for each player (the values)
for record in records:
  scores = []
  players[record[1]] = scores

# Append each score to the appropriate player's list
for record in records:
  if record[1] in players:
    players[record[1]].append(record[-2])

# Sum up the scores for each player and print
for p,q in players.items():
  total = 0 
  for r in q:
    total += (int(r))
  players[p].append(total)
  print(p,q[-1])

# Print all the scores
print('\n', players)