-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRecursive-Power.py
More file actions
23 lines (22 loc) · 844 Bytes
/
Recursive-Power.py
File metadata and controls
23 lines (22 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Author: Jessica Strait
# This project recursively finds the solution of an integer to the power of another integer.
def power(x, n):
# Write your code here
# First I will check to see if both inputs are integers. If they are not, I'll return error.
if type(x) is not int:
return 'error'
elif type(n) is not int:
return 'error'
else:
# If n is negative, we will also return error.
if n < 0:
return 'error'
# If n is exactly zero, we will return one.
if n == 0:
return 1
# If n is exactly one, we return x.
if n == 1:
return x
# Otherwise, we will use a recursive call to multiply x times the power function of x to the next smaller power.
else:
return x * power(x, n-1)