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

Last challenge in "python collection" Can't get through stage one although I get the correct answer on my IDE

I tried this code to solve stage one:

import operator

my_dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections'], 'Me Me': ['My Course']}


def most_classes():
    x = max(my_dict.items(), key=operator.itemgetter(1))[0]
    return x

most_classes()

I got the correct answer on my IDE but not on the challenge. So I went through other students question in the community tab and found this answer:

my_dict = {'Kenneth Love': ['Python Basics', 'Python Collections'], 'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
 'Me Me': ['My Course']}


def most_classes(my_dict):
  max_count = 0
  busy_teacher = ''

  for item in my_dict:
    if len(my_dict[item]) > max_count:
      max_count = len(my_dict[item])
      busy_teacher = item
      print(busy_teacher)


most_classes(my_dict)

But again no luck, I can't finish the first stage successfully. Help is much appreciated. Thanks!

Matthew Hill
Matthew Hill
7,799 Points

The one you found looks pretty legitimate, did you change the code to return the information requested in the challenge and in the format it asks it for? At the moment you're just printing the name of the teacher with the most classes.

4 Answers

Steven Parker
Steven Parker
230,274 Points

:point_right: The challenge wants you to return the teacher with the most classes.

You won't need to print anything.

But you also might want to be sure you've finished checking them all before you return the top choice.

Thanks guys, indeed it was the the print statement that prevented me from finishing the first stage. Although I still don't understand why the first block of code didn't work. It has a return statement, It answers the question at hand and I quote: "should return the teacher with the most classes" and it does. At least on the IDE I work on.

Hi All,

for some reason when I submitting the second stage, I got an error that stage one is not passing anymore. I got back and found that if I click on 'check work' enough times, it will accept my code eventually. I don't know how the system is working in the background but it looks as if the system is calculating my answer differently every time. I can't move forward as stage one will be wrong and I will have to go back. How can solve this issue?

Matthew Hill
Matthew Hill
7,799 Points

Hi Avi, can you post your amended code and the challenges that aren't passing?

Ok, so since my previous comment I managed to pass this stage but at the last stage I ran into a weird problem. I need to create a list of all the values in the dictionary. When I use append() I get this error: "You returned 8 courses, you should have returned 18." When I use insert() I get: "You returned 24 courses, you should have returned 18." Is there another way to add add items into a list without needing to specify an index (since the dictionary has no indexes)?

here is my code:

my_dict = {
    'Kenneth Love': ['Python Basics', 'Python Collections'],
    'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
    'Me Me': ['My Course'],
    'hiroshe': ['My Course'],
}


def most_classes(my_dict):
    max_count = 0
    busy_teacher = ' '

    for item in sorted(my_dict, key=lambda item:len(my_dict[item]), reverse=True):
        if len(my_dict[item]) > max_count:
            max_count = len(my_dict[item])
            busy_teacher = item
    return busy_teacher


most_classes(my_dict)


def num_teachers():
    teach_count = 0

    for key in my_dict:
        teach_count += 1
    return teach_count


num_teachers()


def stats():

    stats_list = []

    for key, value in my_dict.items():
        temp_list = [key, len(value)]
        stats_list.append(temp_list)
    return stats_list

stats()

course_list = []

def courses(my_dict):
    for key, value in my_dict.items():
        course_list.extend(value)
    return course_list

courses(my_dict)

thanks!