Search code examples
pythonfunctiondocumentationdocstring

Python - how to use docstring and code examples of a function


def sum_prime():
digit_Sum([1, 2, 3]) == 1 + 2 + 3 = 6
digit_Sum([5, 6, 7]) == 5 + 6 + 7 = 18
digit_Sum([100, 12, 1]) == 1 + 0 + 1 + 0 + 2 + 1 = 5

**The digit sum function should return the sum of all the digits in a given list of integers. what is wrong with the function signature and doc string? ** please see this image


Solution

    1. Regarding docstring:

    There is no docstring in that function, please refer to Docstring Docs.

    In addition, the explanations given are about digit_Sum, where is the method is sum_prime.

    Either way digit_Sum([100, 12, 1]) == 1 + 0 + 1 + 0 + 2 + 1 = 5 isn't a clear example.

    Here is an example of how docstring could have been written:

    def sum_prime(list):
        """Gets a list and does <OPERATION> on it
    
        Works with list of numbers:
        
        >>> sum_prime([5, 5, 1])
        11
        
        """
        <YOUR CODE HERE>
    
    1. Regarding the signature:

    From what you describe the sum_prime should get a list parameter - so it's signature should be like def sum_prime(list):.

    Hope it helped!