【发布时间】:2018-08-18 10:03:26
【问题描述】:
我已经运行了以下代码sn-p:
#Physics Equations
#Default_Variables
default_path = 10000
default_time = 18000
default_ini_vel = 1
default_acceleration = 1
#Variables
path = default_path
time = default_time
ini_vel = default_ini_vel
acceleration = default_acceleration
#Compute
avg_spd = path / time
velocity = (ini_vel + (acceleration * time))
#Prints
print("Average Speed = " + str(avg_spd))
print("Velocity = " + str(velocity))
我预计代码会返回包含许多小数位的平均速度的浮点类型值。 平均速度的输出等于 0。为什么?
【问题讨论】:
-
我之前已经成功运行过程序。但是当我下次运行它时,问题就开始出现了。
-
除以 / 在 python 3 和 python 2 中的工作方式不同。大概你现在使用 2,其中 / 运算符进行整数除法,即向下舍入到最接近的整数。在这种情况下,为零。您可以通过在除法之前将值转换为浮点数来避免这种情况。
-
谢谢,忘了我有 python 2 和 3。用错了。
标签: python python-3.x python-2.7