Chapter 1, Q 6-8 in Python