Answer:
def power(x, n):
if n == 0:
return 1
else:
return x * power(x, n-1)
print(power(2, 3))
Step-by-step explanation:
Create a function called power that takes two parameters, x and n
If n is equal to 0, return 1
Otherwise, multiply x with the function itself, but as a parameter pass x and n-1 for each iteration.
For example,
If x = 2 and n = 3, Is 3 == 0? No, then power(2, 3) = 2 * power(2, 2) (1)
x = 2 and n = 2, Is 2 == 0? No, then power(2, 2) = 2 * power(2, 1) (2)
x = 2 and n = 1, Is 1 == 0? No, then power(2, 1) = 2 * power(2, 0) (3)
x = 2 and n = 0, Is 0 == 0? Yes, power(2, 0) = 1 (4)
Iteration 4 will give us 1. If you substitute the power(2, 0) in iteration 3 with 1, it becomes 2. If you substitute the power(2, 1) with 2 in iteration 2, it becomes 4. If you substitute the power(2, 2) with 4 in iteration 1, it becomes 8.