### Problem 6 Update your `play` function so that a commentary function is called at the end of each turn. The return value of calling a commentary function gives you the commentary function to call on the next turn. For example, `say(score0, score1)` from the parameters of the function `play` should be called at the end of the first turn. Its return value (another commentary function) should be called at the end of the second turn. Each consecutive turn, call the function that was returned by the call to the previous turn's commentary function.
时间: 2024-01-31 10:01:52 浏览: 113
评分功能小例子
Here is an updated version of the `play` function that implements the required functionality:
```python
def play(strategy0, strategy1, score0=0, score1=0, goal=GOAL_SCORE, say=say):
"""Simulate a game and return the final scores of both players, with commentary."""
player = 0
commentary = say(score0, score1)
while score0 < goal and score1 < goal:
if player == 0:
score0 += take_turn(strategy0(score0, score1), score1, goal)
else:
score1 += take_turn(strategy1(score1, score0), score0, goal)
player = other(player)
commentary = commentary(score0, score1)
return score0, score1
```
We now keep track of the `commentary` function, which is initially set to the commentary function returned by the first call to `say`. At the end of each turn, we call the `commentary` function with the new scores, and update `commentary` to the function it returns, which will be called at the end of the next turn. This continues until the game ends.
阅读全文