Python Advanced #1 - Physics problem of throwing object into the air

In this post, we will examine the solution for the time it takes for an object to reach its highest point if it is thrown vertically into the air.

Since the object is thrown vertically into the air, there is only a y component of the velocity, no x component.  It loses 9.8 m/s (or 32.2 ft/s) of velocity for each second of travel due to gravity.  This example will consider only the deceleration due to gravity and neglect other effects.

This problem can be solved by using the following equation:

final velocity = initial velocity + (acceleration due to gravity * time)

At its highest point, the velocity becomes zero at the point where it is changing from traveling in an upward direction to traveling in a downward direction.  So, if we are interested in knowing the time that it takes for the object to reach its highest point, we know that the final velocity is zero.  The above equation becomes the following:

0 = initial velocity + (acceleration due to gravity * time)

solving for time gives us:

time = (initial velocity / acceleration due to gravity)

Note that the acceleration due to gravity is negative since the object is decelerating which cancels out the negative sign from moving the initial velocity over to the opposite side of the equation.

For coding this problem into Python, we simply ask the user if they are giving us metric or English units and then apply the formula.

In this code, we present a new variable type called float.  This will allow the user to enter a number with decimals as opposed to only integers.

The coding is as follows:


The results of a simulation are as follows:


The above solution gives us the exact amount of time it takes to reach its highest point.  However, to demonstrate another application of the "while" statement in Python, an estimated alternate solution is proposed.  In this solution, we start with the initial velocity and subtract either 9.8 (metric units) or 32.2 (English units) with each pass through the loop.  Additionally we set a counter and increment it by 1 with each pass.  Once we are below zero, we know what increments the solutions falls between.  In this example, we also set the object, "init_vel", to float to allow for the input of decimals.

 The coding is as follows:


The results of a simulation are as follows:



Comments