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 Respuestas
+ 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))