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
zyc233
V2EX  ›  Python

Python for 循环

  •  1
     
  •   zyc233 · Dec 8, 2018 · 3614 views
    This topic created in 2700 days ago, the information mentioned may be changed or developed.
    number=33
    for i in range(3):
    while i:
    guess=int(input('Enter an integer :'))
    if guess==number:
    print('Congratulations, you guessed it.')
    print('(but you do not win any prizes!)')
    break
    elif guess<=number-5:
    print('No, it is a little higher than that')
    elif abs(guess-number)<5:
    print('很接近了!')
    else:
    print('No, it is a little lower than that')
    else:
    print('test')
    print('Done')
    ------------------------------------------------------------
    第一次执行什么参数都没输入为什么会执行>>print('test')
    >>print('test')的缩进级别与 while 一致
    13 replies    2018-12-08 13:37:35 +08:00
    ballshapesdsd
        1
    ballshapesdsd  
       Dec 8, 2018
    longbye0
        2
    longbye0  
       Dec 8, 2018 via Android   ❤️ 1
    代码不是这样贴的啊。

    关键字,循环语句 else,百度下?
    zxcvsh
        3
    zxcvsh  
       Dec 8, 2018 via iPhone
    难得你还知道补一句
    缩进
    bpllzbh
        4
    bpllzbh  
       Dec 8, 2018
    你让我怎么面向游标卡尺编程
    zyc233
        5
    zyc233  
    OP
       Dec 8, 2018
    @longbye0 我有百度过,答案都不是我想要的,我把最后一个[else 的缩进级别提升到 for]&[从 1 开始 for]就不会出现这种情况;就是不太明白为什么 0 就会先执行与 while 缩进级别一致的 else
    zyc233
        6
    zyc233  
    OP
       Dec 8, 2018
    @zxcvsh 新人哈,不太懂怎么 V2EX 的编辑功能
    PS.我连提问后编辑都找不到在哪
    Erichailong
        7
    Erichailong  
       Dec 8, 2018 via Android
    建议撤了,重新贴代码
    ballshapesdsd
        8
    ballshapesdsd  
       Dec 8, 2018
    while i 是啥,是 while 1 么?
    zyc233
        10
    zyc233  
    OP
       Dec 8, 2018
    @ballshapesdsd while 变量 i (字母 i)
    zyc233
        11
    zyc233  
    OP
       Dec 8, 2018
    @Depth 明白,谢谢
    Hzzone
        12
    Hzzone  
       Dec 8, 2018
    ....
    zyc233
        13
    zyc233  
    OP
       Dec 8, 2018 via Android
    @Depth 请问如何避免这种 0 与布尔型变量混用,在这个语句中有哪些方法可以实现
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   832 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 21:54 · PVG 05:54 · LAX 14:54 · JFK 17:54
    ♥ Do have faith in what you're doing.