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

Hussein Amr
Hussein Amr
2,461 Points

can't seem to get through step 4

What's wrong with the code?

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.
dicti = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
         'Kenneth Love': ['Python Basics', 'Python Collections']
         'Zawzki' : ['aaaa','bbbb','cccccc']  
        }
def num_teachers(dicti):
    numbers1 = 0
    for k in dicti.keys():
        numbers1 += 1
    return numbers1

def num_courses(dicti):
    total = 0 
    for courses in dicti:
        total += len(dicti[courses])
    return total

def courses(dicti):
    av_courses = []
    for v in dicti.values():
        av_courses.extend(v)
    return av_courses

def most_courses(dicti):
    course_count = 0
    teacher_name = '' 

    for teacher, courses in dicti.items():
        if len(courses) > course_count:
            teacher_name = teacher
    return teacher_name

2 Answers

Alexandra Barnett
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexandra Barnett
Front End Web Development Techdegree Graduate 46,473 Points

So, you need to find out which teacher has the most courses. Maybe course_count should be changed to max_count so it's visually easier to see.

At the beginning, max_count is 0 because nothing has happened so the highest number of courses is 0. Then you loop over the dictionary. So, lets take the first teacher (A) in that loop - we then compare the number of courses that A has with the max_count to see if A's number of courses is higher than max_count. If A has 3 courses, for example, then it will be higher than max_count which means, currently, A has the highest number of courses so A's number of courses is assigned to max_count as that is now the highest number that other teachers have to beat. If we then go to the next teacher (B), perhaps they only have 1 course, it gets compared to the max_count and it doesn't beat 3 so B does not have the highest number of courses. Finally, if we loop to teacher (C). C has 8 courses and when that is compared to the max_count (which has 3 currently from teacher A), we see that C has more than 3 courses so max_count is assigned to 8 as this is the new highest. The variable max_count is just being used to keep track of the highest number of courses currently and we can use it to check if the next teacher beats that or not.

Hopefully that helps :)

Alexandra Barnett
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexandra Barnett
Front End Web Development Techdegree Graduate 46,473 Points

Hi Hussein! You're nearly there, just one line of code missing - if the course length is greater than course_count, you will need to make course_count equal to the course length (if that makes sense):

    for teacher, courses in dicti.items():
        if len(courses) > course_count:
            course_count = len(courses)
            teacher_name = teacher
    return teacher_name

Let me know if you have any questions! :)

Hussein Amr
Hussein Amr
2,461 Points

Alexandra Barnett I didn't quite understand what you did there :/ can you further explain it please