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

am failing to get the right number of courses

.

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 int(len(teachers))


def num_courses(total):
    for num in total.values():
        return len(num)   

3 Answers

Hi i was stuck in the exact same challnage, ur getting only the last value because u loop through the values but u donโ€™t do nothing with each iteration and u end up returning the last value. i need to declare a counter and add to that count the len.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Since the return statement is indented inside of the for loop, it's the first iteration value that is returned.

Chris Freeman itโ€™s not true, even if he use the return statement in the right spot in the end of the for loop it will not work

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Sorry, I was not commenting on the position of the return statement. I was commenting on your assertion that โ€œur getting only the last value because u loop through the values but u donโ€™t do nothing with each iteration and u end up returning the last value. As is, the loop will execute a single time and return the length of the first value in total.values().

Chris Freeman i didnโ€™t knew that, only the len of the first value?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points
# amending num_courses to print num
>>> def num_courses(total):
...     for num in total.values():
...         print(num)
...         return len(num)
... 

# defining mock data
>>> d = {'a': [1],
...      'b': ['2a', '2b'],
...      'c': ['3a', '3b', '3c'],
...      'd': ['4a', '4b', '4c', '4d'],
... }
>>> num_courses(d)
[1]
1