Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Testing First Steps With Testing Create a Doctest

Doctest code works in python but not on the test.

when I go into python on a separate window and run the test code it works and returns 1.5 like it should. Why am I not getting it right on the test?

average.py
def average(num_list):
    """Return the average for a list of numbers

    >>>list = [1, 2]
    >>>average(list)
    1.5

    """
    return sum(num_list) / len(num_list)

3 Answers

Steven Parker
Steven Parker
231,269 Points

I guess the challenge is pickier than the Python itself.

The challenge wants your test to call the function by passing the test list as a literal and not use an intermediate variable.

The challenge also wants to see a space between the chevrons (>>>) and the function call.


:warning: SPOILER ALERT


def average(num_list):
    """Return the average for a list of numbers

    >>> average([1, 2])
    1.5

    """
    return sum(num_list) / len(num_list)

</div>

Thomas Helms
Thomas Helms
16,816 Points

Yep, I had

>>>average([1, 2])
1.5

and it was giving me fits. I knew I wasn't that far off. Thanks Steven.

Slightly annoying, but thank you.

Steven, thanks for the answer. I had this:

def average(num_list):
    """Return the average for a list of numbers

    >>> av = average([1, 2])
    >>> av == 1.5
    True

    """

    return sum(num_list) / len(num_list)

which also didn't work - grad my answer wasn't too off