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 (Retired) Dictionaries Teacher Stats

Dmitry Karamin
Dmitry Karamin
10,099 Points

Result seems to b right but tells that it's wrong

the task is to make list of lists. i did it, but treehouse tells that it is wrong

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.

def most_classes(my_dict):
    num_of_classes = 0
    for key in my_dict:
        value = my_dict[key]
        if len(value) > num_of_classes:
            num_of_classes = len(value)
            teatcher = key
        else:
            continue
    return(teatcher)

def num_teachers(my_dict):
    teachers = 0
    for key in my_dict:
        teachers += 1

    return(teachers)


def stats(my_dict):
    num_of_classes = 0
    result = []
    for key in my_dict:
        for value in my_dict[key]:
            num_of_classes += 1
        my_list = [key, num_of_classes]
        result.append(my_list)
    return(result)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

I see that the unexpected results were "Didn't get the expected output. Got [['Pasan Premaratne', 4], ['Jason Seifer', 11], ['Andrew Chalkley', 15], ['Kenneth Love', 17], ['Dave McFarland', 18]].". Upon inspection, it appears that the number of courses is monotonically increasing: 4, 11, 15, 17, 18.

It looks like you're not reseting the num_of_classes variable between loops of counting the courses for each teacher.

Also, note you can get the count of the courses by using the len() of the courses list, such as len(my_dict[key])

Dmitry Karamin
Dmitry Karamin
10,099 Points

Oh. so fast reply, Thanks. After i published my question i found out that i didn't reset num_of_classes = 0 in the end of the loop. Now it works. Ok, will try using len() .

I was trying also to use in loop my_dict.values(). but i did'n find the right way. when i code like this: for key, value in my_dict, my_dict.values(): print (key, value)

i got strange output like: (key, key, key) (value, value, value)