python-2.7 - python解释器中的无限循环

标签 python-2.7 infinite-loop python

我是跟着python官方教程一起学的。
我创建了一个斐波那契函数 fib(),如教程所示,
给定参数 1 的函数的输出是(令我惊讶的),
0 的无限轨迹。

>>> def fib(n):
...     a, b = 0, 1
...     while a < n:
...         print a, 
...         a, b = b, a + b
... 
>>> fib(0)
>>> fib(1)
0 0 0 0 0 0 0 0 0 0 (...repeats infinitely, had to break out with ^+Z ...)

我已尝试重现该问题,但未能成功。

>>> def fib(n):
...     a, b = 0, 1
...     while a < n:
...         print a,
...         a, b = b, a + b
... 
>>> fib(0)
>>> fib(1)
0
>>> fib(1)
0

这是一个已知问题还是解释器中的一些临时故障?

最佳答案

我可以重现这个:

>>> def fib(n):
...     a,b = 0,1
...     while a < n:
...         print a,
...         a,b = b, a+b
... 
>>> fib(5)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

我是怎么做到的?上面的代码真的是

def fib(n):
[tab]a,b = 0,1
[tab]while a < b:
[tab][4 spaces]print a,
[eight spaces]a,b = b, a+b

混合制表符和空格会使解释器混淆它应该如何解析缩进。因此,a,b = b, a+b 行实际上并不在 while 循环内,即使它看起来像。

关于python-2.7 - python解释器中的无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14562112/

相关文章:

javascript - 按时间间隔循环 javascript 函数

python - 为什么在 Python 3.6 中计算 f"\{10}"时符号 '{' 仍然存在?

c - 在 switch 语句中使用输入默认值时,程序陷入无限循环

python - 基本 Anagram VS 更高级的 Anagram

python - 解决 ascii codec can't decode byte in position ordinal not in range

python-2.7 - Python 2.7 - 在 subprocess.py 上崩溃

使用 Visual Studio 2012 是否会意外损坏系统文件或个人文件

Windows 中的 Python 配置 - 模块位置 - pip 与 Eclipse/Liclipse

python - 在 Python 中切换 AWS 账户

python - 如何在Windows上分离Python子进程(没有setsid)?