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 Functional Python The Lambda Lambada Recursion

James Barber
James Barber
3,502 Points

Getting a "didn't get the right output" feedback. How do I know what output it is looking for?

I tried my code in a local environment and when I call print(prereqs(courses)) it gives me what I believe should be the right answer (an unordered set of the unique course names in all courses and their pre-reqs in the "courses" dictionary that I passed in: {'Python Collections', 'Setting Up a Local Python Environment', 'Python Basics', 'Django Basics', 'Flask Basics', 'Object-Oriented Python'}

I just can't figure out why this quiz doesn't think that's the right answer. The feedback is insufficient to help me understand what it is looking for...

courses.py
courses = {'count': 2,
           'title': 'Django Basics',
           'prereqs': [{'count': 3,
                     'title': 'Object-Oriented Python',
                     'prereqs': [{'count': 1,
                               'title': 'Python Collections',
                               'prereqs': [{'count':0,
                                         'title': 'Python Basics',
                                         'prereqs': []}]},
                              {'count': 0,
                               'title': 'Python Basics',
                               'prereqs': []},
                              {'count': 0,
                               'title': 'Setting Up a Local Python Environment',
                               'prereqs': []}]},
                     {'count': 0,
                      'title': 'Flask Basics',
                      'prereqs': []}]}


def prereqs(data, pres=None):
    pres = pres or set()
    pres.add(data['title'])
    for prereq in data['prereqs']:
        pres = prereqs(prereq, pres)

    return pres

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

I was also initially stumped on this one until I realized the desired solution is only the prerequisites and does not include the top-level course title. That is, the top course isn't a prerequisite of itself. Solution contains 5 courses.

You may rework your code on your own, if you wish, or here is the solution that worked for me:

def prereqs(data, pres=None):
    pres = pres or set()
    # for each prereq in this courses' prereqs...
    for prereq in data['prereqs']:
        # add title of this prereq course, then...
        pres.add(prereq['title'])
        # use recursive call to find further prerequisites of this
        # course, if any
        prereqs(prereq, pres)
    # return current 
    return pres
Parsenz Dev
Parsenz Dev
13,198 Points

I think the instructions for this particular challenge could be a little more lucid. Also, the error feedback is almost completely unhelpful

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Others, myself included, have misread the challenge. This is the third forum post with this issue. Perhaps a helpful hint could be included in the course.py code:

# Do not include the top level course as a prerequisite 

Kenneth Love, could adding a hint be worthwhile?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yeah, I'll see about updating the prompt. It's an awkward thing to explain.

James N
James N
17,864 Points

Thanks for the help! and BTW, congrats on your recent promotion to staff!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

You're very welcome. Thanks! I am working as a mentor and code reviewer for the exciting new Treehouse Python Web Developer Techdegree program.

James Barber
James Barber
3,502 Points

Awesome, thank you Chris for the help in understanding the issue and Kenneth for trying to make the directions a bit more clear!

I love these python courses and workshops Kenneth. With just your materials, I have made it to level 4 in Google's Foo.Bar python challenge (will attempt level 5 soon)! :)