1def factorial(x):
2 """This is a recursive function
3 to find the factorial of an integer"""
4
5 if x == 1:
6 return 1
7 else:
8 result = x * factorial(x-1)
9 return ()
10
11
12num = 3
13print("The factorial of", num, "is", factorial(num))
1def rec(num):
2 if num <= 1:
3 return 1
4 else:
5 return num + rec(num - 1)
6
7print(rec(50))
1def yourFunction(arg):
2 #you can't just recurse over and over,
3 #you have to have an ending condition
4 if arg == 0:
5 yourFunction(arg - 1)
6
7 return arg
1# Recursive function factorial_recursion()
2
3def factorial_recursion(n):
4 if n == 1:
5 return n
6 else:
7 return n*factorial_recursion(n-1)
8
1houses = ["Eric's house", "Kenny's house", "Kyle's house", "Stan's house"]
2
3# Each function call represents an elf doing his work
4def deliver_presents_recursively(houses):
5 # Worker elf doing his work
6 if len(houses) == 1:
7 house = houses[0]
8 print("Delivering presents to", house)
9
10 # Manager elf doing his work
11 else:
12 mid = len(houses) // 2
13 first_half = houses[:mid]
14 second_half = houses[mid:]
15
16 # Divides his work among two elves
17 deliver_presents_recursively(first_half)
18 deliver_presents_recursively(second_half)
19
1students = { 'Alice': 98, 'Bob': 67, 'Chris': 85, 'David': 75, 'Ella': 54, 'Fiona': 35, 'Grace': 69}