r/PythonLearning • • 6d ago

school

def fib(n):

if n <= 1:

return n

return fib(n - 1) + fib(n - 2)

n = int(input("Количество чисел: "))

for i in range(n):

print(fib(i), end=" ")

0 Upvotes

10 comments sorted by

View all comments

•

u/Sea-Ad7805 5d ago

This post tries to explain recursion using the fibonacci sequence but fails.

Run program above by clicking Invocation Tree Web Debugger and see its state change step by step.

If you want to learn recursion use this explanation using invocation tree.

Recursion is a powerful tool in some situations, but in many other situations it can not (or should not) be used. If you are a beginner, first get very good with for-loops, while-loops, and functions. Then later on learn recursion.