Wrong Output on Running Python Code

Prem Raj

I have run the following code snippet:

#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))

I have expected the code to return a float type value for average speed containing many decimal places. The output for average speed equals 0. Why?

N. Wouda

As others have already observed, the most likely culprit is avg_spd = path / time. In Py2 this is integer division, and the result gets rounded down to the nearest integer. In Py3 this behaviour has changed, and returns the perhaps more intuitive floating-point result.

You can get this 'new' behaviour in Py2 as well, by importing,

from __future__ import division

Above your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related