简体   繁体   English

在 Python 的 while 循环中调用 function 及其先前的返回值作为参数

[英]Calling a function with its previous return value as an argument in a while loop in Python

I need to keep calling the following function with its previous return value as an argument in a while loop:我需要继续调用以下 function 及其先前的返回值作为 while 循环中的参数:

def announce_lead_changes(last_leader=None):
    """Return a commentary function that announces lead changes.

    >>> f0 = announce_lead_changes()
    >>> f1 = f0(5, 0)
    Player 0 takes the lead by 5
    >>> f2 = f1(5, 12)
    Player 1 takes the lead by 7
    >>> f3 = f2(8, 12)
    >>> f4 = f3(8, 13)
    >>> f5 = f4(15, 13)
    Player 0 takes the lead by 2
    """
    def say(score0, score1):
        if score0 > score1:
            leader = 0
        elif score1 > score0:
            leader = 1
        else:
            leader = None
        if leader != None and leader != last_leader:
            print('Player', leader, 'takes the lead by', abs(score0 - score1))
        return announce_lead_changes(leader)
    return say

I understand how the doctest works but how do I implement this in a while loop?我了解 doctest 的工作原理,但是如何在 while 循环中实现它? I tried the following but it keeps on passing the default argument throughout loop:我尝试了以下方法,但它继续在整个循环中传递默认参数:

commentary = both(say_scores, announce_lead_changes())
while
    ...
    commentary(score0, score1)

Update commentary for each iteration in the while loop.更新 while 循环中每次迭代的commentary Try:尝试:

commentary = both(say_scores, announce_lead_changes())
while
    ...
    commentary = commentary(score0, score1)

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

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