Thomas Mullaly

DevOps, Security and IT Leadership

Simple Recursion Example in Python

I generally use loops when solving problems that do the same thing over and over again. I’s starting to use recursion when when a small part of the problem resembles the whole problem. I think I should think of using recursion more often.

AskRecursively.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
def ask_recursively(question):
    reply = input(question)
    reply = reply.strip().lower()

    if reply == 'yes':
        return True
    elif reply == 'no':
        return False
    else:
        print('\nPlease answer "yes" or "no". \n')
        ask_recursively(question)

def main():
    ask_recursively("Do you understand recursion? \n")

if __name__ == '__main__':
    main()

Here’s my attempt at solving the fibonacci sequence using recursion…

Fibonacci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def fibonacci(n):
    if n == 0:
        return n
    elif n == 1:
        return n
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)

def main():
    for x in range(0, 15):
        print(fibonacci(x))

if __name__ == '__main__':
    main()
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377

Here’s a simple example of a recursive factorial method…

Factorial.py
1
2
3
4
5
6
7
8
9
10
11
12
def factorial(n):
    if n <= 1:
        return 1
    else:
        return n * factorial(n -1)

def main():
    for x in range(0,15):
        print(factorial(x))

if __name__ == '__main__':
    main()
1
1
2
6
24
120
720
5040
40320
362880
3628800
39916800
479001600
6227020800
87178291200