推荐学习书目
› 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
qweweretrt515
V2EX  ›  Python

python 全局变量的诡异错误,求助下

  •  
  •   qweweretrt515 · Nov 20, 2016 · 2119 views
    This topic created in 3654 days ago, the information mentioned may be changed or developed.

    http://sharetext.cn/1753


    这是我的全部代码,我运行的时候,出现错误,按道理应该没有问题呀

    Supplement 1  ·  Nov 20, 2016
    已解决
    Supplement 2  ·  Nov 20, 2016
    def echo123():
    global a
    a = a+ 1
    print(a)

    def main1():
    global a
    a = 0
    echo123()

    main1()
    2 replies  •  2016-11-20 14:56:18 +08:00
    DeTamble
    DeTamble
    Nov 20, 2016❤️1
    1
    Python 中当局部变量名字和全局变量名字重复时,局部变量会覆盖掉全局变量。
    而在 echo123() 中没有定义 a ,所以会报 UnboundLocalError: local variable 'a' referenced before assignment
    所以 echo123() 也要加上 global a
    qweweretrt515
    qweweretrt515
    OP
    Nov 20, 2016
    2
    @DeTamble 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   886 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 22:38 · PVG 06:38 · LAX 14:38 · JFK 17:38
    ♥ Do have faith in what you're doing.