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 Dictionary challenge 4 of 5 most_courses

Hi this is my code for this exercise. Whats the difference between looping like how I did it

vs

for key in teacher_dict.keys():
   for value in teacher_dict.values():

vs another way

def most_courses(arg):
    max_count = 0
    teachers = " "
    for teacher, listOfCourses in arg.items():
        if len(listOfCourses) > (max_count):
            max_count = len(listOfCourses)
            teachers = teacher
    return teachers

Also I don't know if its me but I have a real hard problem following how other people write their code. Like if I see codes that are written different than how my though process is I get stump real easily. When I go to stackoverflow to search for something their answere from the forums leave me more confused than before. Does that happen to other newbies or is it just me?

vs other way

def most_courses(teacher_dict):
    max_count = 0
    rockstar = ""
    for teacher, course_list in teacher_dict.items():
        if len(course_list) > max_count:
            max_count = len(course_list)
            rockstar = teacher
    return rockstar
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(teacher_dict):
    count = 0
    for x in teacher_dict:
        count += 1
    return count

def num_courses(teacher_dict):
    value_list = []
    for values in teacher_dict.values():
        for x in values:
            value_list.append(x)

    return len(value_list)

def courses(teacher_dict):
    value_list = []
    for values in teacher_dict.values():
        for x in values:
            value_list.append(x)

    return value_list

def most_courses(teacher_dict): 
    most_course = 0
    #teacher = {"most_course: xxx}
    teacher = "XXXXXXXXX"  
    for key in teacher_dict.keys():
        #print(key): name of teachers

        for value in teacher_dict.values():
            #print(value): courses for each teacher 
            if len(value) > most_course:
                most_course = len(value):
                teacher = str(key)
    print("{} has the max # of course {}!".format(teacher, most_course)
    return teacher

2 Answers

Even My code does not work, any idea why?

def most_courses(teachers_dict): most_course= 0 for key in teachers_dict.keys(): for value in teachers_dict.values(): if len(value)> most_course: most_course=len(value) teacher = str(key) return teacher

teachers_dict = {"Kenneth": ["Happy","Rocky"],"James": ["Happy1","Rocky1","Dotnet"]}

print(most_courses(teachers_dict))

Hey Mohammed, you have to post your code in a specific format for it to show up nicely on these forums.

Sure, will make note of it, going forward. Thanks!