Exercise 1.2 - Solution

(a) The Bouncing Ball

# bounce.py
height = 100
bounce = 1
while bounce <= 10:
    height = height * (3.0/5)
    print bounce, height
    bounce += 1
Commentary

This example illustrates one potential pitfall with mathematical calculations in Python---truncation of integer division. For example, if you use (3/5) instead of (3.0/5) above, you will find that the calculation is meaningless as 3/5 evaluates to 0. This behavior of division is considered to be one of the largest design warts in Python and is being changed in Python 3.

[ Back ]