File tree 1 file changed +18
-1
lines changed
1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 15
15
16
16
def power (base : int , exponent : int ) -> float :
17
17
"""
18
- power(3, 4)
18
+ >>> power(3, 4)
19
19
81
20
20
>>> power(2, 0)
21
21
1
22
22
>>> all(power(base, exponent) == pow(base, exponent)
23
23
... for base in range(-10, 10) for exponent in range(10))
24
24
True
25
+ >>> power('a', 1)
26
+ 'a'
27
+ >>> power('a', 2)
28
+ Traceback (most recent call last):
29
+ ...
30
+ TypeError: can't multiply sequence by non-int of type 'str'
31
+ >>> power('a', 'b')
32
+ Traceback (most recent call last):
33
+ ...
34
+ TypeError: unsupported operand type(s) for -: 'str' and 'int'
35
+ >>> power(2, -1)
36
+ Traceback (most recent call last):
37
+ ...
38
+ RecursionError: maximum recursion depth exceeded
25
39
"""
26
40
return base * power (base , (exponent - 1 )) if exponent else 1
27
41
28
42
29
43
if __name__ == "__main__" :
44
+ from doctests import testmod
45
+
46
+ testmod ()
30
47
print ("Raise base to the power of exponent using recursion..." )
31
48
base = int (input ("Enter the base: " ).strip ())
32
49
exponent = int (input ("Enter the exponent: " ).strip ())
You can’t perform that action at this time.
0 commit comments