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

A quiz question!

Wow, I just can't stump you! OK, two more to go. I think this one's my favorite, though. Create a function named most_courses that takes our good ol' teacher dictionary. most_courses should return the name of the teacher with the most courses. You might need to hold onto some sort of max count variable. What should I write next? With what should I compare the value? Help me please! THANKS! ;)

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 most_courses(g):
    name=""
    num_courses=0
    for h in e.items():
        name=h.keys()
        num_courses= len(h.values)








def courses(e):
    teachers_list=[]
    for f in e.values():
        teachers_list.extend(f)
    return teachers_list






def num_teachers(a):
    teachers=0
    for b in a:
        teachers = teachers+ 1
    return teachers
def num_courses(c):
    courses=0
    for d in c.values():
        courses = courses + len(d)
    return courses

2 Answers

Steven Parker
Steven Parker
231,248 Points

You already asked this question a few hours ago.

Check that link and the answers already given there by Steve Hunter and Jon Mirow.

I know but I didn't understand it completely, could you write something additional for me?

Steven Parker
Steven Parker
231,248 Points

Didn't the answers posted by Steve and Jon help?

They did but I couldn't understand much, so I wrote the code as much as I knew! Check it out. Please help me to solve this! ;)

Steven Parker
Steven Parker
231,248 Points

You have a good start but you have a few issues:

  • after the loop, you still need tor return the "name"
  • you can unpack the dictionary in the loop: "for key, values in e.items():"
  • only update the saved name and count when the count is larger than before

See if you can get it now.

def most_courses(treehouse_teachers): highest_num = 0 teacher_most_classes = "" for key, value in treehouse_teachers.items(): if len(value) > highest_num: highest_num = len(value) teacher_most_classes = key return tearcher_most_classes

it says "Bummer: Couldn't find most_courses" so I don't know why it won't even acknowledge the function's existence

Steven Parker
Steven Parker
231,248 Points

If there's an error in the definition, the function will remain undefined. Formatting is crucial to Python, so always use Markdown code formatting when posting code. Also, to reach the community as a whole always start a new question instead of adding one as an "answer" to another question.