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 trialDean Pierrot
18,403 PointsHaving trouble accessing the courses of the teachers.
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.
# 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 num_teachers(a_single_args):
return len(a_single_args)
def num_courses(a_single_args):
total = 0
for value in a_single_args.values():
for course in value:
total += 1
return total
def courses(a_single_dict):
a_list = []
for course in a_single_dict.values():
a_list.extend(course)
return a_list
def most_courses(a_single_dict):
count = 0
for teacher in a_single_dict:
if len(teacher) > count:
count += 1
else:
return count
1 Answer
Greg Kaleka
39,021 PointsHi Dean,
There are a few things wrong here.
- You don't want to just arbitrarily add 1 to the count. Instead, if the length of the teacher's course list is longer than the current count, simply set the count to the length of the teacher's course list.
- You need to hold on to the teacher's name, since that's what your function should return.
- You're returning the count immediately if a given teacher isn't the longest - don't do that. Check every teacher, then return the longest; that's what the challenge is asking for. Remember to return the name, not the count.
- You're looping through keys in the dictionary, and checking their length. Instead, loop through keys and check the length of the values (which is the course list for that key/teacher). E.g.
len(a_single_dict[teacher])
will give you the count of courses for that teacher. Make sense?
Give it another go, and then add a comment on this answer if you figure it out or if you have more questions.
Cheers
-Greg