简体   繁体   English

Python 3将一个函数传递给另一个函数

[英]Python 3 Passing a Function into Another Function

I am somewhat new to Python, and this is a homework question, so I would appreciate no answers, just advice to help me understand. 我对Python有点陌生,这是一个家庭作业问题,因此我不希望有任何答案,只是建议可以帮助我理解。 I am writing a game program that plays two different strategies against each other - a greedy strategy and a zoom-in strategy, which I have written as function. 我正在编写一个游戏程序,该程序相互之间具有两种不同的策略-贪婪策略和放大策略,它们是我作为函数编写的。 I have a game function that needs to pass in my greedy and zoom-in functions, as well as a game board. 我有一个游戏功能,需要传递我的贪婪和放大功能,以及一个游戏板。 I need to be able to have either strategy function go first. 我需要能够先执行任一策略功能。 So far I can only get it to where my greedy strategy goes first. 到目前为止,我只能把它放在我贪婪的策略首先去的地方。

def game(P1,P2,board):
    P1 = 0
    P2 = 0 
    for x in range(len(board)):
        if x%2 == 0:
           move = greedy(board)
           P1 += board[move]
           board.remove(board[move])
        else:
            move = zoomin(board)
            P2 += board[move]
            board.remove(board[move])
    if P1 > P2:
        return 1
    elif P1 == P2:
        return 0.5
    else:
        return 0

This strategy always assumes that P1 is the greedy function, but I need to be able to play either first. 该策略始终假定P1是贪婪函数,但是我需要能够先玩任何游戏。 I thought I could pass in the functions, so my call would be 我以为可以传递函数,所以我的电话是

game(greedy,zoomin,board)

but I am not sure how to actually implement it so that it can recognize who is playing first. 但是我不确定如何实际实施它,以便它可以识别谁在先玩。

Thank you in advance for your help! 预先感谢您的帮助!

EDIT: 编辑:

Here are my greedy and zoomin functions: 这是我的贪婪和放大功能:

def greedy(board):
    if board[0] > board[len(board)-1]:
        #returns position of first item
        return 0
    elif board[len(board)-1] > board[0]:
        #returns position of last item
        return len(board)-1
    else:
        #if board[-1] == board[0]
        return 0

def zoomin(board):
    if len(board)%2 == 0:
        evens = 0
        odds = 0
        for x in range(len(board)):
            if x%2 ==0:
                evens += board[x]
            else:
                odds += board[x]
        if evens > odds:
            return 0
        else:
            return len(board)-1
    else:
        #choose the larger value (greedy)
        if board[0] < board[len(board)-1]:
            return len(board)-1
        else:
            return 0

This is not a direct answer to your question (since senshin already answered it), but I wanted to point out that you can decrease your code duplication by using arrays instead. 这不是您问题的直接答案(因为senshin已经回答了),但是我想指出,您可以通过使用数组来减少代码重复。 For instance, like this: 例如,像这样:

def game(players, board):
    scores = [0] * len(players)
    while i in range(len(board))
        p = i % len(players)
        move = players[p](board)
        scores[p] += board[move]
        del board[move]    # <-- This is also a faster and more fail-safe version of your "board.remove(board[move])"
    return scores

You can then call this function as game([greedy, zoomin], board) . 然后,您可以将此函数称为game([greedy, zoomin], board) Also note how it extends to an arbitrary number of players, although that may not actually be useful for you. 还要注意它如何扩展到任意数量的玩家,尽管这实际上可能对您没有用。 :) :)

You will want to rewrite your game function slightly. 您将需要稍微重写game功能。 Notice that your game function accepts P1 and P2 , but you don't do anything with them - you immediately assign 0 to both of them. 请注意,您的game函数接受P1P2 ,但是您不对其执行任何操作-您立即为它们两个都分配了0

The correct way to approach this is to have your game function accept two strategies , which can be greedy or zoomin , or whatever else you might come up with later. 解决此问题的正确方法是让您的game功能接受两种策略 ,可以是greedyzoomin ,或者稍后您可能会想到的其他任何方法。

def game(strategy1, strategy2, board):

You will also need to replace the explicit calls to greedy and zoomin in the function body (eg move = greedy(board) ) with calls to the strategies passed into your function instead - something like move = strategy1(board) . 您还需要用对传递到您函数中的策略的调用代替对函数体中对greedyzoomin的显式调用(例如move = greedy(board) ),例如move = strategy1(board)

Then, in order to have greedy play first and zoomin play second, you could call: 然后,为了让greedy首先播放,然后让zoomin播放第二,您可以调用:

game(greedy, zoomin, board)

Or if you wanted zoomin first and greedy second, you could call: 或者,如果您想zoomin然后再greedy ,则可以致电:

game(zoomin, greedy, board)

As you can see, the order of play is determined by the order in which you pass the two strategies into your function. 如您所见,游戏的顺序由您将两种策略传递到函数中的顺序确定。 Let me know if this needs clarification. 让我知道是否需要澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM