Skip to content
scipython.com

P4.1.3Raising an exception on \(0^0\)

Python follows the covention of many computer languages in choosing to define \(0^0=1\). Write a function, powr(a, b) which behaves the same as the Python expression a**b (or, for that matter, math.pow(a,b)) but raises a ValueError if a and b are both zero.

Show solution

Simply check if a and b are both zero and raise an exception if they are:

>>> def powr(a,b):
...     if a == b == 0:
...         raise ValueError('Zero to the power zero')
...     return a**b