推荐学习书目
› 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 · 4110 views
    This topic created in 2894 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
    ballshapesdsd
    Dec 8, 2018
    1
    longbye0
    longbye0
    Dec 8, 2018 via Android❤️1
    2
    代码不是这样贴的啊。

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