Python Using Recursive Fibonacci Sequence

Document 对象参考手册Python3 Examples

The following code uses recursion to generate the Fibonacci sequence:

Example (Python 3.0+)

# Filename : test.py # author by : www.example.com def recur_fibo(n): """Recursive function Output Fibonacci sequence""" if n <= 1: return n else: return(recur_fibo(n-1) + recur_fibo(n-2)) # Get user input nterms = int(input("How many terms do you want to output?")) # Check whether the entered number is correct if nterms <= 0: print("Enter a positive number") else: print("Fibonacci sequence:") for i in range(nterms): print(recur_fibo(i))

The output result after executing the above code is:

您要输出几项? 10
斐波那契数列:
0
1
1
2
3
5
8
13
21
34

Document 对象参考手册Python3 Examples

Other Extensions