Exercise: Functions

Throughout this course, we will be writing many of our own functions. We want to make sure that our functions work correctly- that for any given input, they return the correct output.

One way to tests functions in Python is with doctests. A doctest shows a bunch of calls to a function and the expected output for each. The doctest for a function is always below the function header and before the function body, inside a giant comment known as the docstring.

For example:

def add_two(num1, num2):
    """ Adds two numbers together.

    >>> add_two(4, 8)
    12
    """
    return num1 + num2

Every function should have at least one example in the doctest, but it's better if it has many examples to test different input types and situations. For example, if our function deals with numbers, how does it deal with negative numbers? How about floating point numbers? Let's test!

def add_two(num1, num2):
    """ Adds two numbers together.

    >>> add_two(4, 8)
    12
    >>> add_two(-4, 8)
    4
    >>> add_two(1.1, 1.1)
    2.2
    """
    return num1 + num2

In the exercises, you should first look at the doctests as an additional way of making sure you understand the function you're about to write, and then once written, you should run the tests to make sure that your function works how the doctests expect.

Lifetime Supply Calculator

📺 If you'd like, you can watch how I tackled this first exercise in a walkthrough video.

Or, if you'd like more guidance in written form, expand this block.
  • Your function only needs a single line of code: one return statement.
  • The return statement should contain an arithmetic expression using -, *, (, ).
  • The Boolean expression should involve the parameters (current_age, amount_per_day) as well as literal numbers (100, 365).
For a code template, expand this block.

Fill in the blanks with the appropriate parameters and try running the tests.

return (100 - ______) * (______ * 365)
def calculate_lifetime_supply(current_age, amount_per_day): """ Returns the amount of items consumed over a lifetime (with a max age of 100 assumed) based on the current age and the amount consumed per day. >>> calculate_lifetime_supply(99, 1) 365 >>> calculate_lifetime_supply(99, 2) 730 >>> calculate_lifetime_supply(36, 3) 70080 """ # YOUR CODE HERE

Seconds Between

Implement a function, seconds_between, that returns the number of seconds between years, based on the assumption of 365 days in a year, 24 hours in a day, 60 minutes in an hour, and 60 seconds in a minute.

If you'd like more guidance, expand this block.
  • Your function only needs a single line of code: one return statement.
  • The return statement should contain an arithmetic expression using -, *, (, ).
  • The Boolean expression should involve the parameters (year1, year2) as well as literal numbers (365, 24, 60).
For a code template, expand this block.

Fill in the blank with the appropriate expression and try running the tests.

return (_____________) * 365 * 24 * 60 * 60
def seconds_between(year1, year2): """ Returns the number of seconds between two years. >>> seconds_between(1984, 1985) 31536000 >>> seconds_between(1984, 2000) 504576000 """ # YOUR CODE HERE

Fortune Teller

Implement a function, tell_fortune, that returns a string of the form shown in the docstring and doctests. The string will be a combination of string literals (strings in quotes) and the given parameters.

If you'd like more guidance, expand this block.
  • Your function only needs a single line of code: one return statement.
  • The return statement should contain a string concatenation using the + operator.
  • The Boolean expression should involve the parameters (job, place, partner) as well as a literal string ('You will be a ', ' in ', ' living with ', ' .').
For a code template, expand this block.

Fill in the blanks with the appropriate parameters and try running the tests.

return 'You will be a ' + ____ + ' living in ' + ____ + ' with ' + ____ + '.'
def tell_fortune(job_title, location, partner): """ Returns a fortune of the form: 'You will be a in living with .' >>> tell_fortune('bball player', 'Spain', 'Sonic the Hedgehog') 'You will be a bball player in Spain living with Sonic the Hedgehog.' >>> tell_fortune('farmer', 'Kansas', 'C3PO') 'You will be a farmer in Kansas living with C3PO.' >>> tell_fortune('Elvis Impersonator', 'Russia', 'Karl the Fog') 'You will be a Elvis Impersonator in Russia living with Karl the Fog.' """ return "?" # YOUR CODE HERE

Temperature Converter

Implement two functions, celsius_to_fahrenheit and fahrenheit_to_celsius, to convert temperatures based on the standard conversion formulas. You'll need to modify both the function signature and function body.

Tip: As the final step in the conversion, the built-in round function may be helpful.

def celsius_to_fahrenheit(): # <- COMPLETE THIS """ Returns the Fahrenheit equivalent of the given Celsius temperature. >>> celsius_to_fahrenheit(0) 32 >>> celsius_to_fahrenheit(100) 212 """ # YOUR CODE HERE def fahrenheit_to_celsius(): # <- COMPLETE THIS """ Returns the Celsius equivalent of the given Fahrenheit temperature. >>> fahrenheit_to_celsius(32) 0 >>> fahrenheit_to_celsius(212) 100 """ # YOUR CODE HERE
➡️ Next up: More on Functions