File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 1515
1616def power (base : int , exponent : int ) -> float :
1717 """
18- power(3, 4)
18+ >>> power(3, 4)
1919 81
2020 >>> power(2, 0)
2121 1
2222 >>> all(power(base, exponent) == pow(base, exponent)
2323 ... for base in range(-10, 10) for exponent in range(10))
2424 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
2539 """
2640 return base * power (base , (exponent - 1 )) if exponent else 1
2741
2842
2943if __name__ == "__main__" :
44+ from doctests import testmod
45+
46+ testmod ()
3047 print ("Raise base to the power of exponent using recursion..." )
3148 base = int (input ("Enter the base: " ).strip ())
3249 exponent = int (input ("Enter the exponent: " ).strip ())
You can’t perform that action at this time.
0 commit comments