注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
小弟刚开始学习CS61A,第一个project里面的commentary部分遇到了一点困难
代码如下:
def announce_highest(who, previous_high=0, previous_score=0):
"""Return a commentary function that announces when WHO's score
increases by more than ever before in the game.
>>> f0 = announce_highest(1) # Only announce Player 1 score gains
>>> f1 = f0(11, 0)
>>> f2 = f1(11, 1)
1 point! That's the biggest gain yet for Player 1
>>> f3 = f2(20, 1)
>>> f4 = f3(5, 20) # Player 1 gets 4 points, then Swine Swap applies
19 points! That's the biggest gain yet for Player 1
>>> f5 = f4(20, 40) # Player 0 gets 35 points, then Swine Swap applies
20 points! That's the biggest gain yet for Player 1
>>> f6 = f5(20, 55) # Player 1 gets 15 points; not enough for a new high
"""
assert who == 0 or who == 1, 'The who argument should indicate a player.'
# BEGIN PROBLEM 7
def say(score0, score1):
def print_gain(gain, who, previous_high):
if gain > previous_high:
if gain == 1:
print(gain, "point! That's the biggest gain yet for Player", who)
else:
print(gain, "point! That's the biggest gain yet for Player", who)
return gain
else:
return previous_high
if who == 1:
gain = score1 - previous_score
previous_high = print_gain(gain, who, previous_high)
previous_score = score1
else:
gain = score0 - previous_score
previous_high = print_gain(gain, who, previous_high)
previous_score = score0
return announce_highest(who, previous_high, previous_score)
return say
报错是previous_score是局部量没有定义,但是调用say不应该是可以使用announce_highest中的变量吗?而且教程里面给出的函数好像也是这亚子,不知道为什么。想了一个早上了 求大家帮助 虽然米少但一定会给大家加上
|