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

Benjamin Bradshaw
Benjamin Bradshaw
3,208 Points

Some kinda strangeness is happening.

I'm on task 5 but somehow, whenever I submit my answer it says that task 4 is no longer working. When I go back to task 4 it works just fine. My question is, why is this happening and how does my task 5 look?

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.
teachers = {'Benj': ['Omnisciences'], 'Heather': ['Social Therory'] , 'Chris': ['Game Therory', 'Food Studies'], 'Abe': ['studystudies', 'allstudies']}


def num_teachers(teachers):
    count = 0
    for key in teachers:
        count += 1
    return count

def num_courses(teachers):
    num = 0
    for courses in teachers.values():
        num += len(courses)
    return num

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

def most_courses(teachers):
    max_count = 0
    most_courses = str()
    for teacher, course in teachers.items():
        if len(course) > max_count:
            max_count += len(course)
            most_courses = teacher
        return most_courses

def stats(teachers):
    stat_list = []
    for item in teachers:
        current_teacher = []
        current_teacher = [item, len(teachers[item])]
        stat_list.append(current_teacher)
    return stat_list      

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

When a previously passing task now fails, it usually means that a new syntax error was introduced while working on the current task. In this case, it looks like the return statement for the previous most_courses function has gotten indented inside the for loop.

Also it looks like the max_count is using a += instead of a simple assignment.

Benjamin Bradshaw
Benjamin Bradshaw
3,208 Points

The syntax was the problem. It worked with the += so I'm wondering why I would want to use an assignment instead. should it not have worked? Is there and advantage to using an assignment over a += ?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Revisiting this question got me thinking....

The fact your code passes with the "+= in place is a lucky coincidence due to the order the teachers are retrieved from the teachers dict test code. Running the code with slightly differing data yields wrong results. I added print message to your code to watch for a failing case. Below is a example of a failing case.

>>> def most_courses(teachers):
...     max_count = 0
...     most_courses = str()
...     for teacher, course in teachers.items():
...         print(teacher, course)  # print current teacher and course list
...         if len(course) > max_count:
...             max_count += len(course)
...             print("max_count:", max_count)  # print current max_count
...             most_courses = teacher
...     return most_courses
...

# defining teachers where "jupiter" has the most courses 
>>> teachers = {'venus': ['a1', 'a2'], 'mercury': ['b1', 'b2', 'b3'], 'jupiter': ['c1', 'c2', 'c3', 'c4']}
>>> most_courses(teachers)
venus ['a1', 'a2']
max_count: 2
jupiter ['c1', 'c2', 'c3', 'c4']
max_count: 6
mercury ['b1', 'b2', 'b3']
'jupiter'
# jupiter is seen second while max_count is 2 so it becomes the most_courses teacher

# renaming "jupiter" to "earth", but still with the max courses, changes the dict hash order
>>> teachers = {'venus': ['a1', 'a2'], 'mercury': ['b1', 'b2', 'b3'], 'earth': ['c1', 'c2', 'c3', 'c4']}
>>> most_courses(teachers)
venus ['a1', 'a2']
max_count: 2
mercury ['b1', 'b2', 'b3']
max_count: 5
earth ['c1', 'c2', 'c3', 'c4']
'mercury'
# earth is seen last, but the max_count has already grown to 5 so mercury stays most_courses teacher

You can see from the above example using += has undesired behavior. Using = is the correct solution.

Tagging Kenneth Love to review the challenge test data to be sure to catch the misuse of +=.