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 trialWilhelm Amamba Ngoma
685 PointsExpected 5, got 4
I checked the code attached in my interpreter, It always returned the right number of items. Can you help me figure out why returns 4 instead of 5 expected?
# 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):
max_count = None
for key in dict:
count = len(dict[key])
if max_count is None or max_count < count:
max_count = count
teacher = key
return teacher
def num_teachers(dict):
for value in dict.values():
num_teachers = len(value)
return num_teachers
1 Answer
Hanley Chan
27,771 PointsHi, The question is asking to return the number of teachers in the dictionary.
In your for loop "value" contains a list of courses for a different teacher each iteration. You are resetting "num_teachers" to be len(value) which is the number of courses of a teacher during each iteration of your for loop. In the end "num_teachers" contains the number of courses of the last teacher in the for loop instead of the total number of teachers.
You can get the number of teachers with len(dict)
Wilhelm Amamba Ngoma
685 PointsWilhelm Amamba Ngoma
685 PointsYou're right. I created my own dictionary containing teachers as value. That's why I was wrong. So I altered the function to count the teachers as key instead. That's it. Thanks Hanley Chan.