Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Testing First Steps With Testing Create a Doctest

Darryn Smith
Darryn Smith
32,043 Points

Code not passing Challenge: Create a Doctest. Help, please.

I've been over and over this code and can't figure out why I'm not passing the challenge.

The assignment is: "Add a doctest to average() that tests the function with the list [1, 2]. Because of how we test doctests, you'll need to leave a blank line at the end of your doctest before the closing quotes."

What am I not getting?

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

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

    '''
    return sum(num_list) / len(num_list)

8 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,986 Points

There is a space needed between the >>> prompt and the statement:

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

    >>> average([1, 2])  # <-- added space after prompt
    1.5

    '''
    return sum(num_list) / len(num_list)

Does it matter if you put in a """ or a ''', because for some reason i put everything the same in the challenge but those and when i switched out the """ for the ''' it passed

Chris Freeman
Chris Freeman
Treehouse Moderator 67,986 Points

Either triple-single-quotes or triple-double-quotes should work equally as well. I test the posted answer with both styles and it passes. Perhaps some other typo was introduced.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Darryn;

You are missing a space between your >>> and your average statement. Add that space in there and your code works fine.

Happy coding,
Ken

Darryn Smith
Darryn Smith
32,043 Points

Thanks to both of you. Aiaiaiai, nothing like a space or semicolon to bring the day to a halt!

Cheers!

I'm TOO SLOW!!! Mine keeps getting messed up! I hate these challenges!

I'm not very bright...... def average(num_list): """Return the average for a list of numbers"""

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

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

You are all so much smarter that I will ever be. I'm crying hard right now.

I give up even trying, seriously, what's the point

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

return sum(num_list) / len(num_list)

I erased it all.