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 (Retired) Dictionaries Teacher Stats

I am lost on teacher stat challenge task 1 of 4.

after creating my function def most_classes(dict_teachers): I do not know what to start typing. I understood Kenneth's video on the dictionaries, but I do not understand this challenge. I would appreciate the help. Chris.

teachers.py
# The dictionary will be something like:
 {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(dict_teachers):

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Chris, this passes, but I've never done python before so sorry if its not the best way. Teacher is asking for the full code to return teacher with the most lessons.

def most_classes(dict_teachers):
    teacher = ""
    lessons = 0
    for key, value in dict_teachers.items():
        if (len(value) > lessons):
            teacher = key
            lessons = len(value)
    return teacher

Thanks Damien for replying, but I'm still lost. Such as why the teacher = "", why lessons = 0?

Damien Watson
Damien Watson
27,419 Points

Sure, I'll see if I can clarify, sorry I was just excited that it passed the test.

You pass in a dictionary of teachers and lessons. You have to check through these to find the teacher with the highest lesson count, so I set up a "teacher" variable to store the teacher name with the most lessons and "lessons" variable to store how many lessons they have. Starting at blank teacher = "" and no lessons lessons = 0.

Then you loop through the dictionary one at a time, getting the teachers name and lessons and comparing to what you have stored.

If the length of lessons is greater than the previous teachers lessons, then update the teachers name and lesson count. It only replaces if there are more lessons, so when you get to the end, you have the teacher with most lessons.

Does this help explain?

Its ok, Im a little frustrated with these objectives. It does not seem to me that the objectives are a proper test for us students or its just me not understanding what is being asked from the objectives from just watching the videos. I'm getting stuck on most of the objectives, so I'm thinking about canceling my account with teamtreehouse. But thanks again Damien.

Damien Watson
Damien Watson
27,419 Points

Bad experience possibly? I've done a lot of tutorials on teamtreehouse and it varies from teacher to teacher. A lot of the teachers give simple steps, this example is more hard core than I'd expect. I'd think maybe step 1 is create Method that returns your name. Then the next question should maybe loop through the dictionary.

Like I said I haven't done any Python before, but I have done Javascript, Objective C, Swift, C#, Java, PHP etc, so maybe the coding principles helped me get to the answer.

Stick in there, maybe try a different course or language, or do you need to learn Python straight up?

No, I do not need to learn python straight up at all. I am a single father of two teenagers, and I was just wanting to try my hand at programming, but I'm finding it hard to find time to figure out all these concepts.

Damien Watson
Damien Watson
27,419 Points

Thats pretty full on, well done on finding the time to get on and learn something new.

I started coding 30 years ago, so I have a little head start on you, but my first 'real' language was Javascript, CSS & HTML (web pages). The good thing about them is you get the graphical appreciation, rather than lines of code.

Maybe give one of those a go, the teachers section usually has all the graphics and default files for download.

I have also been looking at the game development courses (Unity 3D) which has a great follow along example of a Frog game. Unity is a free download and one of the major players in game development. Same as others, there is code but you should be able to follow along. I'm building a small game for my daughter, though she lives with her mum, so I have more time on my hands than you would.

I hope you can stick in there and get some success, it is rewarding in the long run.

Aby Abraham
PLUS
Aby Abraham
Courses Plus Student 12,531 Points
def most_classes(dict):
    max_count = 0
    str = ""
    alist = dict.values()
    for key, value in dict.items():
        if len(value) > max_count:
            max_count = len(value)
            str = key
    return str