为什么我的函数在python中使用列表中的所有迭代项

时间:2015-08-27 14:24:51

标签: python python-2.7

我正在尝试建立一个简单的程序来帮助我完成幻想足球选秀。我已经设置了一个" mock"根据一些事情来预测选秀权,但我在将正确的球员存放到合适的球队时遇到了问题。

编辑:问题似乎是它将选择存储到每个团队而不仅仅是我正在迭代的团队。我已经清除了所有旧代码以仅保留新的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):
    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):
    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)

1 个答案:

答案 0 :(得分:-1)

我可以收集的内容,因为问题和代码有点不清楚:

您的缩进似乎是错误的,并且您在循环中声明函数是不必要的

这是我应该做的:

for t in round_order: # t is a class instance / object that is a team with a roster. round_order is a list of the instances in the draft order.
    def pick(b): # I won't go into this here, but this is designed to return the projected pick (which is a class instance / object that is a player, with position info) and it seems to be working.
    d = pick(t)
    print d.name
    remove_pick(d)
    store_pick(d)

def store_pick(z): # this isn't fully fleshed out, but there's enough to get the idea.
                if z.position == "RB":
                    if t.roster["RB1"] == "empty":
                        t.roster["RB1"] = z.name
                    elif t.roster["RB2"] == "empty":
                        t.roster["RB2"] = z.name
                elif pick(t).position == "WR":
                    if t.roster["WR1"] == "empty":
                        t.roster["WR1"] = z.name
                    elif t.roster["WR2"] == "empty":
                        t.roster["WR2"] = z.name
                    elif t.roster["WR3"] == "empty":
                        t.roster["WR3"] = z.name
                elif pick(t).position == "TE":
                    if t.roster["TE"] == "empty":
                        t.roster["TE"] = z.name
def remove_pick(z):
        mock_list.remove(z) # mock_list is a list of instances / objects in the player class. I want to remove it so that player is not available for the next pick.