Python中的斐波那契-简单的解决方案 [英] Fibonacci in Python - Simple solution
本文介绍了Python中的斐波那契-简单的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
n1 = 1
n2 = 1
n3 = n1 + n2
for i in range(10):
n1 + n2
print(n3)
n1 = n2
n2 = n3
据我所知,这应该是输出系列的前10位数字的最简单方法,但是,它会打印 2
10次.我不明白为什么 n1
没有设置为 n2
,为什么 n2
没有设置为 n3 打印
n3
之后的code>.
According to what I know, this should be the simplest way of outputting the first 10 digits of the series, however, it prints 2
10 times. I don't understand why n1
doesn't get set to n2
, and n2
doesn't get set to n3
after n3
has been printed.
推荐答案
您的代码有很多问题.而且,您首先应该自己学习和尝试.我也是一个初学者,所以我知道您在想什么.为了使其快速生效,可以进行一些
There are many issues with your code. And you should first learn and try as much as you can on your own. I am also a beginner so I know what you are thinking. For some quick edits to make it workable:
n1 = 0
n2 = 1
n3 = 0
for i in range(10):
n3 = n1 + n3
print(n3)
n1 = n2
n2 = n3
- 该系列以0开头,您以1进行了初始化.
- 更新语句
n3 = n1 + n2
在循环之外,它将如何更新?此处发生的是n3 = 1 + 1 = 2
,您的代码保持不变,并且没有变化.
- The series starts with 0, you initialized it with 1.
- The update statement
n3=n1+n2
is outside the loop, how will it update? What is happening here isn3 = 1 + 1 = 2
in your code stays the same and it doesn't change.
这篇关于Python中的斐波那契-简单的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文