0

This is a follow-up on a question I posted earlier today, and I think I've cleaned up my code and narrowed down my issue.

I am trying to build a simple program to help me with a fantasy football draft.

The problem seems to be that it is storing the pick to every team instead of just one. This should be done with the store_pick function.

MCVE:

# This is designed as league set up
class Teams(object):
    def __init__(self, name, draft_position, me):
        self.name = name
        self.draft_position = draft_position
        self.me = me
    roster = {"QB" : "empty", "RB1" : "empty", "RB2" : "empty", "WR1" : "empty ", "WR2" : "empty", "WR3" : "empty", "TE" : "empty", "Flex" : "empty", "K" : "empty", "DEF" : "empty"}
    bench = {}
Team1 = Teams("Team1", 1, False)
Team2 = Teams("Team2", 2, False)
Team3 = Teams("Team3", 3, False)

draft_order = [Team1, Team2, Team3]

class Player(object):
    def __init__(self, name, fantasypros, adp, yahoo_rank, myrank, position):
        self.name = name
        self.fantasypros = fantasypros
        self.adp = adp
        self.yahoo_rank = yahoo_rank
        self.myrank = myrank
        self.position = position
    def mock_pick(self):
        return (self.fantasypros + self.adp + self.yahoo_rank)/3.0


player_data = [("Leveon Bell", 1, 1, 5, 1, "RB"), 
               ("Adrian Peterson", 2, 2, 1, 16, "RB"), 
               ("Jamaal Charles", 3, 3, 3, 7, "RB")] 
player_list = [Player(*player) for player in player_data]
mock_list = [Player(*player) for player in player_data]

def store_pick(z, y): #this is where Im having issues
    print z in mock_list
    if z.position == "RB":
        if y.roster["RB1"] == "empty":
            print y.name
            y.roster["RB1"] = z.name
        elif y.roster["RB2"] == "empty":
            y.roster["RB2"] = z.name
    elif z.position == "WR":
        if y.roster["WR1"] == "empty":
            y.roster["WR1"] = z.name
        elif y.roster["WR2"] == "empty":
            y.roster["WR2"] = z.name
        elif y.roster["WR3"] == "empty":
            y.roster["WR3"] = z.name
    elif z.position == "TE":
        if y.roster["TE"] == "empty":
            y.roster["TE"] = z.name
def remove_pick(z):
    print z in mock_list
    mock_list.remove(z)

def grind(l):
    mock_results = []
    grind_count = 0
    while grind_count < len(mock_list):
        for i in l:
            mock_results.append(i.mock_pick())
            grind_count += 1
    return mock_results
def mock_analyzer(l):
    return min(l)
def pick(b): # this chooses a player
    if not t.me:
        for p in mock_list:
            if p.mock_pick() == mock_analyzer(grind(mock_list)):
                if grind(mock_list).count(p.mock_pick()) == 1: #this should figure out if there is a tie
                    if p.position == "RB":
                        if t.roster["RB1"] == "empty":
                            return p
                        elif t.roster["RB2"] == "empty":
                            return p
     else:
         return raw_input("your turn")

# initial draft round code
def mockround(a):
    pick_count = 0
    if a % 2 != 0:
        round_order = draft_order
    else:
        round_order = draft_order.reversed()
    for t in round_order:
        print t.name,
        print "are projected to select"
        d = pick(t)
        print d.name
        remove_pick(d)
        store_pick(d, t)
        pick_count += 1
        print t.name,
        print t.roster
        print Team2.name,
        print Team2.roster
        print Team3.name,
        print Team3.roster




mockround(1)
Ethan
  • 13
  • 4

1 Answers1

0

"roster" is a class attribute. Initialize it in your __init__() method.

Edit: Instead of initializing it with "empty" values for all of the keys, use the defaultdict object. Alternatively, you can have a class object with all of the positions listed, and then use that to pull your keys from.

import collections

class Teams(object):
    def __init__(...):
        # ...
        self.roster = collections.defaultdict()

or

class Teams(object):
    positions = ['QB', 'WR', ...]
    def __init__(...):
        # ...
        self.roster = {pos: None for pos in Teams.positions}
Andy Kubiak
  • 169
  • 6
  • Should I then add the dictionary for the roster to each Team instance? – Ethan Aug 27 '15 at 19:38
  • If each instance of the Teams class should have a different roster.... – Andy Kubiak Aug 27 '15 at 19:47
  • Great, thanks! So then what should I enter in for each team? `self.roster`? (i.e. when I am creating the instances) – Ethan Aug 27 '15 at 19:49
  • Take a look at my edit. I offer two options. You would also do well to check out the question you've duplicated. – Andy Kubiak Aug 27 '15 at 19:51
  • Thank you. I completed what I wanted by adding to teach team the: {pos: None for pos in Teams.positions} code for the roster variable. – Ethan Aug 27 '15 at 19:59
  • You'll need to adjust all of your dict tests, though. So instead of `if self.roster['QB'] == "empty" :`, you can use the more concise and pythonic `if not self.roster['QB']:` – Andy Kubiak Aug 27 '15 at 20:04