0

I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?

  import re, sys, os, math

if len(sys.argv) < 2:
    sys.exit("Usage: %s filename" % sys.argv[0])

filename = sys.argv[1]

if not os.path.exists(filename):
    sys.exit("Error: File '%s' not found" % sys.argv[1])

line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")
line = [line.strip() for line in open(filename)]

f = open (filename)

playerStats = {'players': [0, 0, 0]} 

for players in playerStats:
    player = line.group(1)
    atBat = line.group(2)
    hit = line.group(3)

    if player in playerStats:
            playerStats[player][0] += atBat
            playerStats[player][1] += hit

    if player not in players:
        player = line.group(1)
        playerStats[player][0] = atBat
        playerStats[player][1] = hit
        avgs = 0

    else: 
        playerStats[player][0] = player
        playerStats[player][0] = atBat
        playerStats[player][1] = hit
        playerStats[player][2] = 0

for player in players:
    avgs[player] = round(float(hits[player])/float(atBats[player]), 3) 

print "%s: %.3f" % (player, avgs[player])

Traceback (most recent call last): File "ba.py", line 19, in player = line.group(1) AttributeError: 'list' object has no attribute 'group'

1 Answer 1

3

You should change this

playerStats = {'players': hits, atBats, avgs} 

To

playerStats = {'players': [0, 0, 0]} 

The latter stores the value as a list , the former is not valid Python syntax.

To modify one of these values you would do, for example

playerStats[player][1] = 5   # atBat value

You could also change to a nested structure like

playerStats = {'players': {'hits' : 0,
                           'atBats' : 0,
                           'avgs' : 0)}

Then you could modify the values as

playerStats[player]['hits'] = 3
Sign up to request clarification or add additional context in comments.

6 Comments

Changing to tuple fixed the syntax error, thanks! Now it says hits, atBats, and avgs arent't defined in the tuple, but I create them there then define them later right? How can I get around that
You would initialize as playerStats = {'players': [0,0,0]}, then you modify the values using indexing like playerStats[player][0] += 3. Also, I just realized I goofed, tuples are immutable, so you'd have to go with the list to modify the members. Or go with the nested dictionary method.
Could you please edit your original post with this code? It is very difficult to read code within the comment section, all the formatting is lost.
I edited it up top. Getting "atBats is undefined" when running the script
Okay I see the problem, change that line to playerStats = {'players': [0, 0, 0]}
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.