📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
查看: 3954| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

CS61A怎么会这个亚子?出师不利,hog函数调用问题

全局:
公开课
学校名称: UCB
Unit号: 1
开课时间: 2018Spring
课程全名: CS61A
平台: 其他
URL: https://inst.eecs.berkeley.edu/~cs61a/sp18/proj/hog/

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

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中的变量吗?而且教程里面给出的函数好像也是这亚子,不知道为什么。想了一个早上了 求大家帮助 虽然米少但一定会给大家加上



上一篇:AWS Certified Solutions Architect Associate 证书经验总结
下一篇:Ph.D工作两年转行DS 学习计划打卡
🔗
 楼主| doggie3689 2019-6-2 08:39:19 | 只看该作者
全局:
是因为问题描述不清楚吗 求大家帮助啊
网址如下:
https://inst.eecs.berkeley.edu/~ ... hog/#problem-7-2-pt
回复

使用道具 举报

🔗
 楼主| doggie3689 2019-6-2 09:27:11 | 只看该作者
全局:
又花了半天总算搞懂了…………
在local frame中,不能对上一层的frame中的变量进行赋值操作,如果这样做,python会默认是当前local中的新建变量,从而前面的调用就会出现没有定义的错误。
虽然自己解决了,但也感谢看了问题的各位。
希望能帮上有同样问题的同学!
回复

使用道具 举报

全局:
关于这个当时也困扰了我一下, 有两种方案:
其一是nonlocal关键字, 具体的使用用法可以简单的查一下官方文档.官方文档的nonlocal
其二则是曲线救国, 我们虽然不能对上一层frame的变量进行赋值操作,换言之re-assign name-binding, 但是读取变量的值是允许的.你可以将麻烦的值根据需要作为expression的一部分计算后绑定到当前frame的变量.
个人推荐第二种方案, 因为nonlocal关键字的使用在课程后面会教的,而且有时候用起来也有点tricky, 小心被练习题里的弄混了. 不过相对个人比较薄弱的recursion来说, nonlocal关键字的题目我觉得还是比较简单的啦.

评分

参与人数 1大米 +1 收起 理由
doggie3689 + 1 感谢帮助

查看全部评分

回复

使用道具 举报

🔗
AndyMoriaty 2019-11-24 18:19:50 | 只看该作者
全局:
@葱葱ATQB 61A的问题呢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表