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 trialKevin Faust
15,353 Pointsteacher stats. challenge task 1/4
Challenge Task 1 of 4
Create a function named most_classes that takes a dictionary of teachers. Each key is a teacher's name and their value is a list of classes they've taught. most_classes should return the teacher with the most classes.
def most_classes(d):
max_count = {"teacher" : 0};
for teachers in d:
teacher_count = len(teachers.values())
if teacher_count > max_count.values():
max_count = {teachers : teacher_count}
return max_count
I keep getting Bummer! 'str' object has no attribute 'values'
2 Answers
Chris Freeman
Treehouse Moderator 68,454 Points # for each <key> in <dict>
for teachers in d:
# <var> = length of <dict>.values()
teacher_count = len(teachers.values())
Your teachers
is a dict key of type <string> Perhaps you meant:
# for each <key> in <dict>
for teacher in d: # make key char singular (esthetics)
# <var> = length of <dict>[<key>]
teacher_count = len(d[teacher])
Nicholas Lim
Python Development Techdegree Student 6,055 PointsThis one was very confusing to me because sometimes my code would pass and sometimes it wouldn't. I think if there was a better sample from the beginning of what data we were supposed to take in I could've done it a lot quicker.
def most_classes(dict):
for teachers in dict:
for x in dict:
if len(dict[teachers]) > len(dict[x]):
key = teachers
elif len(dict[teachers]) < len(dict[x]):
key = x
return key
Could someone tell me what is wrong with this solution?
Kevin Faust
15,353 PointsKevin Faust
15,353 Pointsthanks. so we can only call values() on the dictionary itself to get all the values in the dictionary. We cant just call it on a key?
To access the value of a key, we only can do dictionary[key]?
Kevin Faust
15,353 PointsKevin Faust
15,353 PointsI got the challenge to pass with this:
Is there a better way to do this
Chris Freeman
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsCorrect.
.values()
is a dict method that returns all the values in dict as a list.The value of a key is the key itself. More precisely, dictionary[key] returns the value stored in the dict at the "address" made by hashing key