zyc233
V2EX  ›  Python

Python for 循环

By zyc233 at 2018 年 12 月 8 日 · 3981 次点击
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 条回复  •  2018-12-08 13:37:35 +08:00
ballshapesdsd
   1
ballshapesdsd  
   2018 年 12 月 8 日
longbye0
   2
longbye0  
   2018 年 12 月 8 日 via Android   ❤️ 1
代码不是这样贴的啊。

关键字,循环语句 else,百度下?
zxcvsh
   3
zxcvsh  
   2018 年 12 月 8 日 via iPhone
难得你还知道补一句
缩进
bpllzbh
   4
bpllzbh  
   2018 年 12 月 8 日
你让我怎么面向游标卡尺编程
zyc233
   5
zyc233  
OP
   2018 年 12 月 8 日
@longbye0 我有百度过,答案都不是我想要的,我把最后一个[else 的缩进级别提升到 for]&[从 1 开始 for]就不会出现这种情况;就是不太明白为什么 0 就会先执行与 while 缩进级别一致的 else
zyc233
   6
zyc233  
OP
   2018 年 12 月 8 日
@zxcvsh 新人哈,不太懂怎么 V2EX 的编辑功能
PS.我连提问后编辑都找不到在哪
Erichailong
   7
Erichailong  
   2018 年 12 月 8 日 via Android
建议撤了,重新贴代码
ballshapesdsd
   8
ballshapesdsd  
   2018 年 12 月 8 日
while i 是啥,是 while 1 么?
zyc233
   10
zyc233  
OP
   2018 年 12 月 8 日
@ballshapesdsd while 变量 i (字母 i)
zyc233
   11
zyc233  
OP
   2018 年 12 月 8 日
@Depth 明白,谢谢
Hzzone
   12
Hzzone  
   2018 年 12 月 8 日
....
zyc233
   13
zyc233  
OP
   2018 年 12 月 8 日 via Android
@Depth 请问如何避免这种 0 与布尔型变量混用,在这个语句中有哪些方法可以实现
推荐学习书目
› 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
© 2026 V2EX · 32ms · 3.9.8.5