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

Martin Batista
Martin Batista
4,606 Points

my stats function task 5 of 5 does not pass...

my code:

def stats(d): teacher = [] courses = [] list_of_lists = [] for key in d.keys(): teacher.append(key) for value in d.values(): courses.append(len(value))

    for num in range(0, len(teacher)):
        list_within = [teacher[num], courses[num]]
        list_of_lists.append(list_within)

return list_of_lists

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(d):
    return len(d.keys())


def num_courses(d):
    total_courses = []
    for value in d.values():  # value in  dictionary passed as argument
        for val in value:
            total_courses.append(val)
    return len(total_courses)  # returns total number of courses

def courses(d):
    total_courses = []
    for value in d.values():
        for val in value:
            total_courses.append(val)
    return total_courses

def most_courses(d):
    courses_num_list = []
    teachers_list = []
    indexes = []
    all_indexes = []
    most_teachers = []

    for courses in d.values():
        courses_num = len(courses)
        courses_num_list.append(courses_num)
    for teachers in d.keys():
        teachers_list.append(teachers)

    count = 0
    for num in courses_num_list:
        if count == 0:
            high_val = num
            count += 1
        else:
            if num > high_val:
                high_val = num
            else:
                pass

    indexes = [index for index, value in enumerate(courses_num_list) if value == high_val]
    for i in indexes:
        index = i
        most_teachers.append(teachers_list[index])

    for i in most_teachers:
        return i


def stats(d):
    teacher = []
    courses = []
    list_of_lists = []
    for key in d.keys():
        teacher.append(key)
        for value in d.values():
            courses.append(len(value))

        for num in range(0, len(teacher)):
            list_within = [teacher[num], courses[num]]
            list_of_lists.append(list_within)

return list_of_lists

1 Answer

Indentation Problem in stats function: See comments for more details

def stats(d):
    teacher = []
    courses = []
    list_of_lists = []
    for key in d.keys():
        teacher.append(key)
    #reduce indentation
    for value in d.values():
        courses.append(len(value))
    #reduce indentation
    for num in range(0, len(teacher)):
        list_within = [teacher[num], courses[num]]
        list_of_lists.append(list_within)
    #increase indentation
    return list_of_lists