-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path122.py
31 lines (23 loc) · 987 Bytes
/
122.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#In Factorial.py, we provide a recursive Factorial function
#in Python. Now, let's see a version of it that makes it
#a little more clear what's going on.
#Notice that the core code of this file is no different from
#Factorial.py, but there are print statements throughout to
#let us see what the program is doing. We store the results
#of n * factorial(n - 1) in the variable result so that we
#can print it before returning.
def factorial(n):
if n == 1:
print("Base case reached!")
return 1
else:
print("Finding ", n, " * ", n-1, "!", sep = "")
result = n * factorial(n - 1)
print(n, " * ", n-1, "! = ", result, sep = "")
return result
#Now let's run it again! You can change this line to see
#how different calculations change the output.
print("5! is", factorial(5))
#Notice in the output how the execution climbs down the
#ladder from the original value of n to 1, then climbs
#back up the latter from 1 to n.