Getting infinite loop in fibonacci series in Python

偶尔善良 提交于 2019-12-02 07:25:37

range = raw_input() sets range to be a string, e.g. it is assigning range = '5' rather than range = 5.

The comparison third < range will therefore always be True in Python 2.x *, as integers always compare less than strings:

>>> 10 < '5'
True

The minimal fix is to convert the input to an integer:

range = int(raw_input())

However, note that range is a built-in function, so you should pick a different name for that variable.

* Note that in 3.x comparing a string with an integer causes an error:

>>> 10 < '5'
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    10 < '5'
TypeError: unorderable types: int() < str()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!