Exercise: Function argument’s default values

3.4.7.1. Exercise: Function argument’s default values#

Let’s repeat the timing gate exercise, but this time using a proper function.

A sprinter runs through two timing gates spaced by 50 m as shown in Fig. 3.1. Each timing gate records the time (in seconds) at which the sprinter passes through it.

Write a function named calculate_speed that takes two mandatory arguments, which are the time of each timing gate, and that returns the mean velocity of the sprinter between gates 1 and 2, so that calling:

print(calculate_speed(1.3, 6.7))

prints a value of 9.2593.

In addition, this function should accommodate alternate distances between the timing gates, specified by an optional argument named distance_gates12:

calculate_speed(1.3, 6.7, distance_gates12=75)

Do not forget to include a docstring to your function.

Hide code cell content
def calculate_speed(time_gate1, time_gate2, distance_gates12=50):
    """
    Calculate the average velocity between two timing gates.

    Parameters
    ----------
    time_gate1, time_gate2 : float
        Time at which the athlete passed through the gate, in seconds.
    distance_gates12 : float
        Optional. Distance between both timing gates, in meters. Default is 50.

    Returns
    -------
    float
        The velocity, in m/s.

    """

    return distance_gates12 / (time_gate2 - time_gate1)


# Test the function:
print(calculate_speed(1.3, 6.7))
print(calculate_speed(1.3, 6.7, distance_gates12=75))
9.25925925925926
13.888888888888888