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

Bob Pickles
Bob Pickles
1,363 Points

Confused about why my code isn't working.

I am having a bit of an issue:

The challenge says to return a single list with all the available courses. I have this code which looks to return the correct answer in workspaces but it's failing in the challenge:

I found someone else's courses function and tried it. However, I don't understand why it works but mine doesn't. The output looks the same in workspaces.

My code

def courses(teachers):
  new_list=[]
  for course in teachers.values():
    for c in course:
      new_list.append(c)
  return new_list

Their code

def courses(teach_dict):
    class_list = []
    for teacher in teach_dict:
        for course in teach_dict[teacher]:
            class_list.append(course)
    return class_list

Output

['Python Basics', 'Python Collections', 'jQuery Basics', 'Node.js Basics']       <--theirs                
['Python Basics', 'Python Collections', 'jQuery Basics', 'Node.js Basics']      <--mine

I even did a boolean comparison by running both functions side by side:

print(courses(teachers) == courses2(teachers)) 

Thinking maybe there was something hidden that I wasn't seeing but that returned True telling me they are both the same.

What am I doing wrong?

Steven Parker
Steven Parker
231,269 Points

Can you provide a link to the course page where this challenge is?

3 Answers

Steven Parker
Steven Parker
231,269 Points

This code is different from what you listed the first time. It has an indentation error on the 2nd line from the bottom.

That line is part of a loop and should be indented more than the "for" line above it.

Bob Pickles
Bob Pickles
1,363 Points

Ok, I feel kinda stupid now, not sure how I missed the indentation error. Must have happened when I pasted the code in from the workspace. Thanks!

Steven Parker
Steven Parker
231,269 Points

I guessed that you were working on task 3 of the Teacher Stats challenge.

I pasted your code directly into the challenge, and it passed. The one thing I noticed is that the indentation is different between your two examples, and since this is just one of several tasks, all tasks should use consistent indentation.

Bob Pickles
Bob Pickles
1,363 Points

Steven,

Thank you for the reply. You are correct, task 3 or Teacher Stats. I just attempted the challenge again and got the same failure using my code

def courses(teachers):
  new_list=[]
  for course in teachers.values():
    for c in course:
      new_list.append(c)
  return new_list

I'm a bit confused about how you're getting a different response. The indentation was just a product of where I was pasting from. My code was being pasted from the workspace whereas the working code was pasted directly from the challenge.

Bob Pickles
Bob Pickles
1,363 Points

This is what's currently in my editor:

# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
def num_teachers(teachers):
    return len(teachers.keys())

def num_courses(teachers):
    count=0
    for course in teachers.values():
        count += len(course)
    return count    

def courses(teachers):
    new_list=[]
    for course in teachers.values():
        for c in course:
        new_list.append(c)
    return new_list