anon_swe
anon_swe

Reputation: 9335

Basic Python Dictionary of String Keys and Int Values

I'm writing a very basic script that calculates an NFL player's fantasy football points based on last year's full-season data. When I do this, I look at the number of games they will miss this year, and apply that discount factor to their total points.

E.g. Tom Brady will miss 4 games this year so he loses 25% of his points.

Since so few players are going to miss games, I have a dictionary that maps a player's name to the number of games he'll miss. When I create players, I check whether his name is in the SUSPENSIONS dictionary like so:

if name in SUSPENSIONS.keys():
    newTE = TE(name, team, rushingYards, rushingTouchdowns, rushingFumbles, receptionYards, receptionTouchdowns, SUSPENSIONS[name])

I tested this with Antonio Gates, who's going to miss 4 games this year. His discount of 25% isn't being applied. I run the above code and then do:

print(str(newTE.gamesSuspended))

and get back 0.

Here's my SUSPENSIONS dictionary:

SUSPENSIONS = {"Antonio Gates": 4, "Ahmad Bradshaw": 1, "Trey Watts": 4, "Tom Brady": 4, "Josh Gordon": 16, "Le'Veon Bell": 2, "LeGarrette Blount": 1}

A few interesting things:

1) Tom Brady's discount does get applied although Le'Veon Bell's, Antonio Gates, and presumably most others don't.

2) My code knows that "Antonio Gates" is a key in the SUSPENSIONS dictionary. For some reason, it sets his gamesSuspended field to 0 instead of 4 (the value corresponding to his name in the dictionary). Why is this?

Any help in sorting this out would be much appreciated, bclayman

Edit to show TE.py:

from Player import *
from constants import *

class TE(Player):
    def __init__(self, name, team, rushingYards, rushingTouchdowns, rushingFumbles, receptionYards, receptionTouchdowns, position="TE", gamesSuspended=0):
        super().__init__(name, team, rushingYards, rushingTouchdowns, rushingFumbles, gamesSuspended)
        self.receptionYards = receptionYards
        self.receptionTouchdowns = receptionTouchdowns
        self.position = "TE"

    def calculatePoints(self):
        return (super().calculatePercentageOfSeason() * (super().calculatePoints() + POINTS_PER_RECEPTION_YARD * self.receptionYards + POINTS_PER_RECEPTION_TOUCHDOWN * self.receptionTouchdowns))

    def toString(self):
        return "position: " + self.position + " " + super().toString() + " rushing yards: " + str(self.rushingYards) + " rushing touchdowns: " + str(self.rushingTouchdowns) + " rushing fumbles: " + st    r(self.rushingFumbles) + " reception yards: " + str(self.receptionYards) + " reception touchdowns: " + str(self.receptionTouchdowns)

So TE(...) is a constructor the Tight End class. If gamesSuspended variable doesn't get bound, it's initialized to 0 (since it's an optional parameter). But I do pass in gamesSuspended...

Upvotes: 1

Views: 54

Answers (1)

John Coleman
John Coleman

Reputation: 51998

You are passing the gamesSuspended for the player's position, which I don't think you want.

With so many parameters in the contructor, sometimes it is useful to use named parameters as to not get them mixed up.

newTE = TE(name, team, rushingYards, rushingTouchdowns, rushingFumbles, receptionYards,
           receptionTouchdowns, position="TE", gamesSuspended=SUSPENSIONS[name])

Upvotes: 3

Related Questions