Skip to content

Commit 681e02a

Browse files
authored
Update power_using_recursion.py
1 parent 3e51abd commit 681e02a

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

maths/power_using_recursion.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,27 @@ def power(base: int, exponent: int) -> float:
1717
"""
1818
Calculate the power of a base raised to an exponent.
1919
20-
Examples:
2120
>>> power(3, 4)
2221
81
2322
>>> power(2, 0)
2423
1
2524
>>> all(power(base, exponent) == pow(base, exponent)
2625
... for base in range(-10, 10) for exponent in range(10))
2726
True
27+
>>> power('a', 1)
28+
'a'
29+
>>> power('a', 2)
30+
Traceback (most recent call last):
31+
...
32+
TypeError: can't multiply sequence by non-int of type 'str'
33+
>>> power('a', 'b')
34+
Traceback (most recent call last):
35+
...
36+
TypeError: unsupported operand type(s) for -: 'str' and 'int'
37+
>>> power(2, -1)
38+
Traceback (most recent call last):
39+
...
40+
RecursionError: maximum recursion depth exceeded
2841
"""
2942
return base * power(base, (exponent - 1)) if exponent else 1
3043

0 commit comments

Comments
 (0)