Unsupported operand type(s) for ** or pow()

def x1(ap,dp,ph,e1,e2,e3,e4,e5,y):
if Delta(ap,dp,ph,e1,e2,e3,e4,e5,y) >= 0:
return 2*(b(ap,dp,ph,e1,e2,e3,e4,e5,y)3) - 9ab(ap,dp,ph,e1,e2,e3,e4,e5,y)c(ap,dp,ph,e1,e2,e3,e4,e5,y) + 27(a2)d(ap,dp,ph,e1,e2,e3,e4,e5,y)
else:
return None
def y1(ap,dp,ph,e1,e2,e3,e4,e5,y):
if Delta(ap,dp,ph,e1,e2,e3,e4,e5,y) >= 0:
return 3
np.sqrt(3)anp.sqrt(Delta(ap,dp,ph,e1,e2,e3,e4,e5,y))
else:
return None

This is creating a problem

return np.sqrt(x1(ap,dp,ph,e1,e2,e3,e4,e5,y)**2+y1(ap,dp,ph,e1,e2,e3,e4,e5,y)**2)

TypeError: unsupported operand type(s) for ** or pow(): ‘NoneType’ and ‘int’

I think the main problem is due to the condition for Delta>=0 which returns a None value for some input dataset but I only need the Delta>=0 condition values

Hi Dark.

Sorry, your post isn’t clear to me. You seem to be pointing out a
problem with your code, namely:

  • you return None, and pass it to another function and try to
    perform mathematical operations on it

but it’s not clear what you want us to do about that. x1() and y1()
are your functions, I think. If you don’t want them to return None,
don’t return None.

(An old joke comes to mind, about a fellow who goes to the doctor and
says, “Doc, it hurts when I do this!” and the doctor replies “Then don’t
do that.”)

You can do that three ways:

  • don’t pass in input values that will give you negative Delta;

  • raise an exception (perhaps ValueError?) if Delta is negative;

  • return some other value that makes more sense (perhaps math.nan)?

Alternatively, the caller can test for None and do something else.

Only you can decide which approach is best for your task.