You are here

Exercises

8 September, 2015 - 10:43

Code examples from this chapter are available from http://thinkpython.com/code/Time1.py; solutions to these exercises are available from http://thinkpython.com/code/Time1_soln.py.

Exercise 16.6. Write a function calledmul_timethat takes a Time object and a number and returns a new Time object that contains the product of the original Time and the number.

Then usemul_timeto write a function that takes a Time object that represents the finishing time in a race, and a number that represents the distance, and returns a Time object that represents the average pace (time per mile).

Exercise 16.7. Thedatetimemodule providesdateandtimeobjects that are similar to the Date and Time objects in this chapter, but they provide a rich set of methods and operators. Read the documentation athttp: // docs. python. org/ 2/ library/ datetime. html.

  1. Use thedatetimemodule to write a program that gets the current date and prints the day of the week.
  2. Write a program that takes a birthday as input and prints the user’s age and the number of days, hours, minutes and seconds until their next birthday.
  3. For two people born on different days, there is a day when one is twice as old as the other. That’s their Double Day. Write a program that takes two birthdays and computes their Double Day.
  4. For a little more challenge, write the more general version that computes the day when one person is n times older than the other.