我怎样才能让python打印出不是“ nan”的数字

时间:2019-09-27 16:51:39

标签: python python-3.x math equation

所以我试图制作一个程序,用用户必须提供的一些输入来计算一个非常高级的方程式,尽管当我打印变量时,它只是显示为nan

代码:

import os
import math

os.system("CLS")

m = float(input("Mass (in kg): "))
r = float(input("Radius (in m): "))
A = math.pi*r**2
k = 0.5*1.22*0.75*A
I = float(input("Impulse of engine (in seconds): "))
T = float(input("Thrust of engine (in Kn): ")*1000)
t = I/T
g = 9.82
GravForce = m*g
q = math.sqrt((T-m*g)/k)
x = 2*k*q/m
p = 1.22

v = q*(1-math.exp(-x*t))/(1+math.exp(-x*t))
yb = (-m/(2*k))*math.log((T-m*g-k*v**2)/(T-m*g))
yc = (+m/(2*k))*math.log((m*g+k*v**2)/(m*g))

print("Velocity at burnout =",v)
print("Boost Phase Distance =",yb)
print("Coast Phase Distance =",yc)
print("Total Altitude =",yb+yc)

谢谢您的帮助,我通过固定输入而不是输入T来解决它,然后* 1000我删除了该输入,并以牛顿代替千牛顿来输入。

2 个答案:

答案 0 :(得分:4)

问题是下面的代码行

T = float(input("Thrust of engine (in Kn): ")*1000)

将其更改为

T = float(input("Thrust of engine (in Kn): ")) *1000

答案 1 :(得分:0)

此代码T的求值为inf,因为它试图将字符串转换为浮点型。

  

input("Thrust of engine (in Kn): ")*1000)是重复的1000次输入字符串,并尝试强制转换为浮点型。
  str类型无法转换为浮点类型,因此math将其评估为inf
  math在计算涉及nan时返回inf

import os
import math

os.system("CLS")

m = float(input("Mass (in kg): "))
r = float(input("Radius (in m): "))
A = math.pi*r**2
k = 0.5*1.22*0.75*A
I = float(input("Impulse of engine (in seconds): "))
T = float(input("Thrust of engine (in Kn): "))*1000
t = I/T
g = 9.82
GravForce = m*g
q = math.sqrt((T-m*g)/k)
x = 2*k*q/m
p = 1.22

v = q*(1-math.exp(-x*t))/(1+math.exp(-x*t))
yb = (-m/(2*k))*math.log((T-m*g-k*v**2)/(T-m*g))
yc = (+m/(2*k))*math.log((m*g+k*v**2)/(m*g))

print("Velocity at burnout =",v)
print("Boost Phase Distance =",yb)
print("Coast Phase Distance =",yc)
print("Total Altitude =",yb+yc)