[英]iPython crashes on printing Fibonacci Series
While attempting to print a fibonacci series using tuples, iPython tends to crash.在尝试使用元组打印斐波那契数列时,iPython 往往会崩溃。
Here is the code I am trying to execute.这是我试图执行的代码。
n = raw_input("Please enter a number: ")
a = 0
b = 1
while b < n:
(b,a) = (a,b+a)
print b
However, if I replace n with a number (eg. 20, 100, 1000), it runs smoothly.但是,如果我将 n 替换为一个数字(例如 20、100、1000),它会运行顺利。 I also tried to run this code in Pycharm, with similar results.
我还尝试在 Pycharm 中运行此代码,结果相似。 Pycharm ran the code, with a huge stream on numbers being generated, and a warning which read:
Pycharm 运行了代码,生成了大量关于数字的流,并显示了一条警告:
Too much output to process
需要处理的输出过多
What causes this crash?是什么导致了这次崩溃?
You forgot to turn the string n
you get from raw_input
into an integer.您忘记将从
raw_input
获得的字符串n
转换为整数。
Since the comparison is done by type name in this case b < n
will always be True
.由于在这种情况下比较是按类型名称完成的,因此
b < n
将始终为True
。
Use n = int(raw_input("Please enter a number: "))
使用
n = int(raw_input("Please enter a number: "))
因为raw_input的返回值是str,你应该试试n = int(raw_input("Please enter a number: "))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.