3.1. Exercises

Docstring Questions

def factor_of_10(number):
    """
    Takes a number and determines if it is a factor of 10 
    Parameters
    ----------
    number : int
        the value to check
      
    Returns
    -------
    bool
        Returns True if numbers are a multiple of 10 and False otherwise
      
    Examples
    --------
    >>> factor_of_10(72)
    False
    """

    if number % 10 == 0: 
        factor = True
    else:
        factor = False
        
    return factor
def add_stars(name):
  """
  This will return your input string between a pair of stars. 
  Parameters
    ----------
    name: str
        a sentence or word
        
    Returns
    -------
    str
        The initial string beginning and ending with a pair of stars 
        
    Examples
    --------
    >>> add_stars('Good Job')
    'Good Job'
    """

    name = '**' + name + '**'
    return

Which Docstring is Most Appropriate?

Given the function below:

def acronym_it(sentence):
    words = sentence.split()
    first_letters = [word[0].upper() for word in words]
    acronym =  "".join(first_letters)
    return acronym
"""
A function that converts a string into an acronym of capital
letters

Input
------
str : sentence
   The string to obtain the first letters from
    
Output
-------
str
    A string of the first letters of each word in an uppercase format
    
Sample
-------
>>> acronym_it("Let's make this an acronym")
"LMTAA"
"""
"""
A function that converts a string into an acronym of capital
letters

Input 
------
some_words : str
   The string to obtain the first letters from
    
Output 
-------
list 
    A list of the first letters of each word from the input string

Example
-------
>>> acronym_it("Let's make this an acronym")
"LMTAA"
"""
"""
A function that converts a string into an acronym of capital
letters

Parameters
----------
sentence : str
   The string to obtain the first letters from
    
Returns
-------
str
    a string of just first letters in an uppercase format

Example
-------
>>> acronym_it("Let's make this an acronym")
"LMTAA"
"""
"""
A function that converts a string into an acronym of capital
letters


Returns
-------
list :
    A list of the first letters of each word from the input string
    
Parameters
----------
str : sentence
   The string to obtain the first letters from
    
Example
-------
>>> acronym_it("Let's make this an acronym")
"LMTAA"
"""

Practice Writing a Docstring

Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

In module 5, we wrote a function that returns the BMI given a person’s weight and height. Let’s write a docstring for it now!

def bmi_calculator(height, weight):
    return (weight / (height ** 2)) * 702

Tasks:

  • Write a NumPy style docstring for the function provided.
  • For this question, we want you to write your docstring between 3 single quotations ''' instead of the normal double quotations """. This will allow us to test the solution provided.
  • Make sure to include a brief description, parameters, return, and example sections.
  • View the documentation of the function.
Hint 1
  • Are you using ''' to contain your docstring?
  • Are you including all the sections?
  • Are you getting the documentation of the docstring using ?bmi_calculator
Fully worked solution: