V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
AzureDeer
V2EX  ›  Python

请问大家,我这一段为啥报错呀

  •  
  •   AzureDeer · 2021-01-07 07:04:00 +08:00 · 3328 次点击
    这是一个创建于 1199 天前的主题,其中的信息可能已经有所发展或是发生改变。

    scores = {'语文':89, '数学':95, '英语':80} sum_score = 0

    def get_average(scores): for subject, score in scores.items(): sum_score += score print('现在的总分是%d'%sum_score) ave_score = sum_score/len(scores) print('平均分是%d'%ave_score)

    get_average(scores)

    11 条回复    2021-01-07 15:42:52 +08:00
    AzureDeer
        1
    AzureDeer  
    OP
       2021-01-07 07:04:28 +08:00
    scores = {'语文':89, '数学':95, '英语':80}
    sum_score = 0

    def get_average(scores):
    for subject, score in scores.items():
    sum_score += score
    print('现在的总分是%d'%sum_score)
    ave_score = sum_score/len(scores)
    print('平均分是%d'%ave_score)

    get_average(scores)
    jmc891205
        2
    jmc891205  
       2021-01-07 07:50:20 +08:00 via iPhone   ❤️ 1
    报的啥错
    cominghome
        3
    cominghome  
       2021-01-07 08:06:52 +08:00
    原因出在 sum_score 上。
    你不赋值的时候,sum_score 是可以当成全局变量使用的,否则这个值会被当成局部变量使用。
    解决办法有两个:
    1. 在 get_average 内部将 sum_score 声明为全局变量
    ```
    def get_average(scores):
    global sum_score
    ...
    ```
    2. 在 get_average 内部声明 sum_score
    ```
    # 删掉
    def get_average(scores):
    sum_score = 0


    ```
    cominghome
        4
    cominghome  
       2021-01-07 08:08:31 +08:00
    我靠,双击空格直接发出去了。。不过应该已经讲清楚了。。

    变量作用域这一块可以读一下《流畅的 Python 》第七章
    Perry
        5
    Perry  
       2021-01-07 08:09:51 +08:00 via iPhone
    请先学会如何提问以及如果通过报错找到错误。
    kikikiabc
        6
    kikikiabc  
       2021-01-07 08:11:31 +08:00 via iPhone
    用了全局变量,函数里面要加一个 global sum_score 声明。
    lpts007
        7
    lpts007  
       2021-01-07 08:26:04 +08:00 via Android
    python 工作不多了,go 吧
    XIVN1987
        8
    XIVN1987  
       2021-01-07 09:58:02 +08:00
    sum_score 不能用全局变量吧,难道你每次调用 get_average()都要累计之前的分数?
    那你计算平均分的时候只除以本次调用的科目数,,平均分岂不是越来越高、单调递增了~~~^_^~~~
    sxbug01
        9
    sxbug01  
       2021-01-07 12:47:55 +08:00
    用函数返回值啊
    huichao
        10
    huichao  
       2021-01-07 14:21:18 +08:00
    def get_average(scores):
    sum_score = 0
    for subject, score in scores.items():
    sum_score += score
    print('现在的总分是%d' % sum_score)
    ave_score = sum_score / len(scores)
    print('平均分是%d' % ave_score)

    get_average(scores)
    tanwill12138
        11
    tanwill12138  
       2021-01-07 15:42:52 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2963 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 03:11 · PVG 11:11 · LAX 20:11 · JFK 23:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.