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 Collections (2016, retired 2019) Dictionaries Teacher Stats

angel moreta
angel moreta
2,912 Points

ok i need a hint

challenge:That one wasn't too bad, right? Let's try something a bit more challenging. Create a new function named num_courses that will receive the same dictionary as its only argument. The function should return the total number of courses for all of the teachers.

I know the challenge says I don't have to create a dict, but i added one to the code "my_teachers" so you guys can see more clearly my point of view here. Something else I'll drop down below the output I'm getting from my terminal and I am also using my text editor ATOM, since i don't feel myself comfortable interacting in the workspaces.

with: total = every_course + every_course -------- output >>> 4 with just: total = every_course ----------- output >>>> 2 can someone explain me why?

teachers.py
# 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)
my_teachers = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
 'Kenneth Love': ['Python Basics', 'Python Collections']}

def num_courses(courses):
    for every_course in courses.values():
        total = every_course + every_course
        return len(total)
print(num_courses(my_teachers))

1 Answer

Couple of things I see here.

First is that your return is inside your for loop which means it's only going to go through a single iteration before it's returned out.

Second is when you do every_course + every_course inside your for loop your basically doubling up the current value. Say for the first iteration of the loop every_course equaled ['jQuery Basics', 'Node.js Basics']. Then every_course + every_course would equal ['jQuery Basics', 'Node.js Basics', 'jQuery Basics', 'Node.js Basics'].

Maybe create the total variable before the loop and add the length of each value that is iterated to that total :)

    total = int()
    for current_courselist_in_iteration in courses.values():
        total += len(current_courselist_in_iteration)
    return total