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

Python Collections most_courses Challenge Questions

My function, most_courses, is returning the last item in a dictionary instead of the item with the most values even though my loop's if statement is asking change the teacher variable to the current teacher only if that current teacher's value is greater than the previous teacher's values stored in counter.

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)

def num_courses(teachers):
    number = []
    for value in teachers.values():
        number += value
    return len(number)

def courses(teachers):
    course=[]
    for value in teachers.values():
        course+=value
    return course


def most_courses(teachers):
    counter=0
    teach=""
    for teacher,value in teachers.items():
        if len(value)>counter:
            teach=teacher
            counter=len(value)
            return teach

2 Answers

Since your code exits at the return statement your loop will never get past the first time len(value)>counter. Outdent the return statement so it lines up with the for statement.

Thank you, Kris!