12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

158 Chapter 16. Classesand functions16.6 Glossaryprototype and patch: Adevelopmentplanthatinvolveswritingaroughdraftofaprogram,testing,and correcting errorsas they arefound.planned development: A development plan that involves high-level insight into the problem andmore planning than incremental development or prototype development.pure function: A function that does not modify any of the objects it receives as arguments. Mostpure functions arefruitful.modifier: A function that changes one or more of the objects it receives as arguments. Most modifiersare fruitless.functional programming style: A style of program design in which the majority of functions arepure.invariant: A condition that should always be trueduring theexecution of aprogram.16.7 ExercisesExercise16.6 Writeafunctioncalledmul_timethattakesaTimeobjectandanumberandreturnsanew Time object that contains theproduct ofthe original Time and the number.Then use mul_time to write a function that takes a Time object that represents the finishing timein a race, and a number that represents the distance, and returns a Time object that represents theaverage pace (timeper mile).Exercise 16.7 Write a class definition for a Date object that has attributes day, month and year.Writeafunctioncalledincrement_datethattakesaDateobject,dateandaninteger,n,andreturnsa new Date object that represents the day n days after date. Hint: “Thirty days hath September...”Challenge: does your function deal with leap years correctly? See wikipedia.org/wiki/Leap_year.Exercise 16.8 The datetime module provides date and time objects that are similar to the Dateand Time objects in this chapter, but they provide a rich set of methods and operators. Read thedocumentation atdocs.python.org/lib/datetime-date.html.1. Use the datetime module to write a program that gets the current date and prints the day ofthe week.2. Write a program that takes a birthday as input and prints the user’s age and the number ofdays, hours, minutes and seconds until their next birthday.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!