python - 在 Python 的 while 循环中调用一个函数,并将其先前的返回值作为参数
问题描述
我需要继续调用以下函数,并将其先前的返回值作为 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
我了解 doctest 的工作原理,但是如何在 while 循环中实现它?我尝试了以下方法,但它继续在整个循环中传递默认参数:
commentary = both(say_scores, announce_lead_changes())
while
...
commentary(score0, score1)
解决方案
更新commentary
while 循环中的每次迭代。尝试 :
commentary = both(say_scores, announce_lead_changes())
while
...
commentary = commentary(score0, score1)
推荐阅读
- c# - 如何继承c# windows forms应用程序中的设计部分?
- java - NumberPicker 与数据库中的数组
- r - 当我申请 quanteda.corpora 的英国政党宣言语料库时,为什么 quanteda 的 textmodel_wordfish 会无限运行?
- c# - 从 :this() 继承是什么意思?
- javascript - Protractor : 从 Popup 值中选择一个元素
- linux - 查找通过setsid创建的进程组id
- dart - 颤振键盘重叠问题无法通过 resizeToAvoidBottomPadding 解决
- html - 如何使用角度反应将表单数据发布到期望常规 HTML 表单的服务器?
- wordpress - 动态 meta_query 取决于 url Wordpress 自定义帖子类型中的查询变量参数
- html - 在 ASP:Gridview 中居中 ASP:Linkbutton