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

Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

please somebody can help me out to explain to me

i am stucked here 2 days already. i can return the maximum value put not the name of the teacher. please somebody can explain what i should do

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(value):
    teachers = []
    for x in value:
        teachers.append(x)
    return len(teachers)

def num_courses(value):
    cours = []
    for a in value.values():
        for b in a:
            cours.append(b)
    return len(cours)

def courses(value):
    lesson = []
    for a in value.values():
        for b in a:
            lesson.append(b)
    return lesson

def most_course(args):
    name []
    for a in args.values():
        name.append(len(a))
    return max(name)

2 Answers

Hi there - have a look at this and let me know if it works. Good luck!

def most_courses(t_dict):
    course_count = 0
    for k in t_dict.keys():
        if course_count < len(t_dict[k]):
                course_count = len(t_dict[k])
                teacher = k
    return teacher
Oszkár Fehér
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Oszkár Fehér
Treehouse Project Reviewer

it's working but i don't really understand what is happening there and i don't want to continue till i understand. thank you for the code

I have added some comments and changed a few things : hopefully makes it more readable. If not ping me a question and I will try my best to help.

def most_courses(t_dict):
    # a temporary integer to hold which teacher has the most courses 
    most_courses = 0
    # for each teacher loop through the teacher dict that was passed in to the function
    for teacher in t_dict.keys():
        # the first tes, most_courses is 0 we test this against the length of 
        # how many items in the associated teachers list via len(t_dict[teacher])
        # for kenneth Love this gives a value of 3 while Andrew Chalkey this gives a 4
        if most_courses < len(t_dict[teacher]):
            # assign the value of items in the associated teachers list to 
            # most_courses in this case 4 will be assigned as Andrew is the 
            # first teacher in the dict
            most_courses = len(t_dict[teacher])
            # next we assign the teacher to the variable we will return
            # in this case we have assigned Andrew Chalkey 
            teacher_with_most_courses = teacher            
    return teacher_with_most_courses

if __name__ == "__main__":

    teachers = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics', 'Java Advanced', 'JS.Node'],
        'Kenneth Love': ['Python Basics', 'Python Collections', 'Java Basics']}
    teacher = most_courses(teachers)
    print(teacher)