Unsupported operand types ‘NoneType’

Tags: ,



I am very new to Python, and I’m trying to create a software that calculates a series of equations and I keep running into an error.

This is the code:

import math

x_i = 1
dt = 0.01
k = 11.5
r_out = 0.889
w = 0.03175
dL = 30
m = float(input("Enter m: "))
v_i = float(input("Enter v_i: "))

t = [0]
x = [x_i]
v = [v_i]
Fc = []

while (v_i > 0):
 
    Fc.append(k*v_i**2*math.cos(math.atan(30/x_i)))/2*(math.sqrt(r_out**2-(w/math.pi)*math.sqrt(x_i**2+dL**2)))
    x.append(x_i+v_i*Dr+0.5*(-Fc_I/m)*dt)
    v.append(v_i-(k*v_i**2*math.cos(math.atan(30/x_i)))/2*(math.sqrt(r_out**2-(w/math.pi)*math.sqrt(x_i**2+dL**2))))
    v_i = v_i + 1
    t.append(dt*(t+1))

My goal is that the code runs these equations until v_i equals 0, while populating the lists with the relevant results, then throwing all the information into a graph once the while loop is done.

However, after inserting m and v_i, I get an error that says:

line 19, in <module>
    FC.append(k*v_i**2*math.cos(math.atan(30/x_i)))/2*(math.sqrt(r_out**2-(w/math.pi)*math.sqrt(x_i**2+dL**2)))
TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

I’m a bit stuck at the moment and I can’t seem to figure out how to fix this error.

Answer

This line:

C.append(k*v_i**2*math.cos(math.atan(30/x_i)))/2*(math.sqrt(r_out**2-(w/math.pi)*math.sqrt(x_i**2+dL**2)))

You are appending a value to C, and then using the / operator on the append() call.

list.append() returns None, hence the error. Try this instead:

C.append(k*v_i**2*math.cos(math.atan(30/x_i))/2*(math.sqrt(r_out**2-(w/math.pi)*math.sqrt(x_i**2+dL**2))))

P.S. PEP-8 is recommended:

C.append(k * v_i ** 2 * math.cos(math.atan(30 / x_i)) / 2 * (math.sqrt(r_out ** 2 - (w / math.pi) * math.sqrt(x_i ** 2 + dL ** 2))))


Source: stackoverflow