TypeError - cannot multiply 'float' by 'NoneType'

1.5k views Asked by At

I have a function which defines an integral.

An array A[i,j] is later defined by multiplying said function by 0.5/pi.

I get the error:

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

Clearly this is because I am multiplying something of no type by a number, which python cannot interpret.

What would you all suggest is the easiest fix to this error?

I have included my code below (does not include all code, so sum variables might seem undefined)

def integral_normal(p_i, p_j):
"""Evaluates the contribution of a panel at the center-point of another,
in the normal direction.

Arguments
---------
p_i -- panel on which the contribution is calculated.
p_j -- panel from which the contribution is calculated.

Returns
-------
Integral over the panel of the influence at a control-point.
"""
def func(s):
    return ( (+(p_i.xc-(p_j.xa-sin(p_j.beta)*s))*cos(p_i.beta)
              +(p_i.yc-(p_j.ya+cos(p_j.beta)*s))*sin(p_i.beta))
            /((p_i.xc-(p_j.xa-sin(p_j.beta)*s))**2
              +(p_i.yc-(p_j.ya+cos(p_j.beta)*s))**2) )
    return integrate.quad(lambda s:func(s), 0., p_j.length)[0]

# computes the source influence matrix
A = np.empty((N_panels, N_panels), dtype=float)
np.fill_diagonal(A, 0.5)

# use enumerate to access element panels individually from 0 to i, locates element of A to fill.
for i, p_i in enumerate(panels):
    for j, p_j in enumerate(panels):
        if i != j:
            A[i,j] = 0.5/pi*integral_normal(p_i, p_j)

# computes right hand side of linear system
b = - u_inf * np.cos([p.beta for p in panels])

# solves the linear system
sigma = np.linalg.solve(A, b)

for i, panel in enumerate(panels):
    panel.sigma = sigma[i]

Here is the error:

TypeError                                 Traceback (most recent call last)
<ipython-input-101-b5c8b9263c2d> in <module>()
      26     for j, p_j in enumerate(panels):
      27         if i != j:
----> 28             A[i,j] = 0.5/pi*integral_normal(p_i, p_j)
      29 
      30 # computes right hand side of linear system

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
1

There are 1 answers

1
bjornsen On

Assuming this is the entire definition of integral_normal(), you are never actually returning a value from the function. By default, if you do not have a return statement, python will return None, meaning your recursive call to integral_normal(p_i, p_j) on line 28 will always return None. That's a lot of words to say "You need to actually return a value."