0
How to print Fibonacci sequence using loops.
I'm confused about how to print the Fibonacci sequence using loops. Like, what is the non-iterable value supposed to be if i decide to use for loop?
2 Answers
+ 1
# Hi! Iâm not shure what you mean with âthe non iterable valueâ, but a loop is a good way to solve the problem, since the values are built on earlier result values.
# So if you donât want just the end result, you can just print out the subtotals in every loop.
# There are a lot of Fibonacci code to look at if you search for them. Hereâs one that including a loop, and where the results is added to a list, and build up a Fibonacci number sequence:
def fib(n):
l = [0, 1]
for i in range(n):
l.append(sum(l[i:i+2]))
return l
print(fib(5))