【发布时间】:2021-12-11 09:07:57
【问题描述】:
我有一个函数score0,它给我一个循环中的每一圈(n 圈)的分数。这个分数每回合增加一个从 1 到 15 的随机整数。
我现在必须设计另一个更高阶的函数,它应该打印玩家在所有得分跳跃中的最高得分跳跃,并且应该在score0 函数中调用。我把它命名为highest_gain。当然,这应该打印第一个得分值,因为它是第一轮(因此它是最大的跳跃)。
# Function that defines the highest point jump in a score yet
import random
def highest_gain(previous_value, highest_point):
def say(score) :
if previous_value == 0:
print ('Biggest gain by player0 yet with',score,'points!')
return highest_gain(score, score)
gain = score - previous_value
if gain > highest_point:
print('Biggest gain by player0 yet with',score,'points!')
return highest_gain(score, gain)
return say
# Function that gives me a new score (incremented) every turn
def score0(n, score = 0):
while n > 0:
score += random.randint(1, 15)
highest_gain(previous_value = 0,highest_point = 0)(score)
n -= 1
return score
#Calling the function
score0(4,0)
问题是调用highest_gain() 不会更新previous_value 和highest_point 的值。为什么这些变量没有在 score0() 函数体中更新,应该如何调用 highest_gain() 以便在循环的每次迭代中更新这些变量?
【问题讨论】:
-
为了清楚起见,您认为变量应该在哪里更新?
-
应该在
score0函数体中更新
标签: python python-3.x higher-order-functions