Base Case

Double-click this starter code to paste it into the interpreter:

def factorial(n):
    """Compute n! where n is an integer >= 0."""

#Test cases:
0 |> factorial |> print  # 1

Let’s start the function by taking care of the base case, when n == 0.

Start a case block and make the first match statement match 0 which will check if n matches to 0. According to the definition of factorial, 0! = 1. Therefore, the program should return 1 if n matches to 0.

Run the code to make sure the test cases pass!

Show Solution:


Next
Previous