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 trialTuomas Martikainen
2,868 PointsMy problem with some of the challenges
My problem with many of the challenges is that it's not clearly stated what's expected to be returned. For example with this one:
"most_classes" should return the teacher with the most classes.
The way I understand this, is that I have to return the name of the teacher with most classes? I checked the other topics about this and it seems that one has to return a list(?) instead of just the name. I don't understand where it states in the challenge description about returning a list?
Anyway, this is how I approached the challenge. I tried with my own set of teachers and classes and it works. (returns the name of the teacher with most classes)
# 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_classes = 0
teacher_name = ''
for teacher in dict:
if len(teacher) > max_classes:
teacher_name = teacher
max_classes = len(teacher)
return teacher_name
2 Answers
Jeff Wilton
16,646 PointsI agree that sometimes the challenges are confusing, but in this case the issue is a bit of confusion in how to retrieve items from a dictionary. You are looping through the dict and getting the keys, but then you need to use each key to get the list of items from their dict entries:
def most_classes(dict):
max_classes = 0
teacher_name = ''
for teacher in dict:
if len(dict[teacher]) > max_classes:
teacher_name = teacher
max_classes = len(dict[teacher])
return teacher_name
You can also retrieve both the key and value while looping through the dict like this:
def most_classes(dict):
max_classes = 0
teacher_name = ''
for teacher, classes in dict.items():
if len(classes) > max_classes:
teacher_name = teacher
max_classes = len(classes)
return teacher_name
Tuomas Martikainen
2,868 PointsSeems like the challenge has been revised, passed it and understood it correctly now. Also thanks Jeff, I understood how you can retrieve both key and value at the same time from your example. Thank you.