V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
xshell
V2EX  ›  Python

一个 python 很简单的问,但是和书上运行的结果不一样,求科普

  •  
  •   xshell · Oct 15, 2015 · 2971 views
    This topic created in 3854 days ago, the information mentioned may be changed or developed.
    x = 0
    for i in [1,2,3,4,5]:
    if x == i:
    continue
    x += i
    print "x:", x

    我在 python2.7 下运行 x 的值是 5 ,但是书上说 x 的结果是 12~~~

    这是什么问题呢·,··

    += 这个符号在 python2 的运算符中没有找到
    bl2fu
        1
    bl2fu  
       Oct 15, 2015
    逻辑上是 12
    anjouslava
        2
    anjouslava  
       Oct 15, 2015
    12 ,估计你是缩进错了, x+= i 放在 for 循环之外了
    x = 0
    for i in [1,2,3,4,5]:
    if x == i:
    continue
    x += i #注意这里的缩进
    print "x:", x
    anjouslava
        3
    anjouslava  
       Oct 15, 2015
    在线运行结果 http://codepad.org/4brvhGvb
    TimePPT
        4
    TimePPT  
    PRO
       Oct 15, 2015   ❤️ 1
    请购买游标卡尺 :)
    xshell
        5
    xshell  
    OP
       Oct 15, 2015
    @anjouslava @bl2fu @TimePPT
    谢谢三位,果然是缩进的问题·
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   759 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 21:29 · PVG 05:29 · LAX 14:29 · JFK 17:29
    ♥ Do have faith in what you're doing.