Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

Virginia Hickman
Virginia Hickman
1,541 Points

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

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 51,960 Points

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.