Python Python Collections Dictionaries Teacher Stats

Kudakwashe Chatikobo
Kudakwashe Chatikobo
1,010 Points

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

bot .net
bot .net
11,466 Points

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 55,457 Points

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

bot .net
bot .net
11,466 Points

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 55,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().

bot .net
bot .net
11,466 Points

Chris Freeman i didn’t knew that, only the len of the first value?

Chris Freeman
Chris Freeman
Treehouse Moderator 55,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