If else statement unsupported operand type(s) for *: 'NoneType' and 'float'

101 Views Asked by At

I'm using if else statements in Jupiter notebook to vary the value of parameters which are then used in a series of differential equations. This is my current set up...

def epsilon1(t, V1, E_v1, I_v1, A_v1, R_v1, N1):
if t < 28:
    return 0 
if 29 < t < 84:
    return 0.006 * N1 / (N1 - (V1 + E_v1 + I_v1 + A_v1 + R_v1))
if 85 < t < 105:
    return 0.001 * N1 / (N1 - (V1 + E_v1 + I_v1 + A_v1 + R_v1))
if t > 104:
    return 0 
if V1 + E_v1 + I_v1 + A_v1 + R_v1 > 0.4 * N1:
    return 0

Then I set up my differential equations as follows:

def deriv(y, t, N...
Lambda1 = beta11 * (I_s1 + delta_a * A_s1 + delta_v * delta_a * A_v1 + delta_v * I_v1 + delta_i * I_i1 + delta_a * delta_i * A_i1)/N1 + beta12 * (I_s2 + delta_a * A_s2 + delta_v * delta_a * A_v2 + delta_v * I_v2 + delta_i * I_i2 + delta_a * delta_i * A_i2)/N2 + beta13 * (I_s3 + delta_a * A_s3 + delta_v * delta_a * A_v3 + delta_v * I_v3 + delta_i * I_i3 + delta_a * delta_i * A_i3)/N3 + beta14 * (I_s4 + delta_a * A_s4 + delta_v * delta_a * A_v1 + delta_v * I_v4 + delta_i * I_i4 + delta_a * delta_i * A_i4)/N4
 epsilon_current1 = epsilon1(t, V1, E_v1, I_v1, A_v1, R_v1, N1)
 dS1dt = - Lambda1 * S1 - epsilon_current1 * S1

This worked with my previous code which was the same apart from the parameter only had one condition. Now i'm getting this error code...

--> 217     dS1dt = - Lambda1 * S1 - epsilon_current1 * S1
    218     dS2dt = - Lambda2 * S2 - epsilon_current2 * S2
    219     dS3dt = - Lambda3 * S3 - epsilon_current3 * S3

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

The set up for epsilon2 and epsilon3 is the same as epsilon1 just with different values.

1

There are 1 best solutions below

0
Jan On

Just at the very top of your function you could implement a check, e.g.:

Lambda1 = 1.0; S1 = 2.0; epsilon_current = None

if any(x is None for x in [Lambda1, S1, epsilon_current]):
    print("There's something fishy about these variables, really.")
    # some cool error handling here