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
Melissa Castle
15,560 PointsPython Dictionaries most_classes function
This code "works" depending on what key of the teacher dictionary it's on, i.e., I click check work, and I receive the message "bummer: returned one teacher, but expecting a different teacher" (although it gives me the actual names.
So, where I set teach_name looks to me like it's in the right scope, but apparently it's not because if I click the check work button again, the returned name changes and if I keep clicking it, I eventually get to the correct name and the task passes. But, of course, when I go to task 2, it fails again. this seems very strange for a couple of reasons, but the main one is: where I set teach_name = key, why is that changing?
def most_classes(teach_dict):
max_count = 0
teach_name = ''
for key in teach_dict:
for cnt in teach_dict.values():
if len(cnt) > max_count:
max_count = len(cnt)
teach_name = key
return teach_name
6 Answers
Melissa Castle
15,560 Pointsthanks Kenneth. so, then shouldn't this code work? doesn't classes contain the list of values for the current key?
def most_classes(teach_dict):
max_count = 0
count = 0
teach_name = ''
for key in teach_dict:
for classes in teach_dict[key]:
count = len(classes)
if count > max_count:
max_count = count
teach_name = key
Kenneth Love
Treehouse Guest TeacherDictionaries are not guaranteed to be accessed in the same order every time. You're doing two loops through your dictionary so you're likely getting two different orders of content, which means you're looking at the courses for a different teacher than the name that you're on. Since you have the name and the name is the key, use that to look up the classes that you should be counting.
Melissa Castle
15,560 PointsThanks Kenneth. I think that's where I'm getting stuck.
does key, as in ( for key in teach_dict: ) have a value of " 'Kenneth Love': ['Python Basics', 'Python Collections'] " or just " ['Python Basics', 'Python Collections'] "?
Kenneth Love
Treehouse Guest TeacherIf teach_dict is something like:
{
'Kenneth': [1, 2, 3],
'Jason': [4, 5, 6],
'Ben': [7, 8, 9]
}
Then in
for key in teach_dict:
key will be either 'Kenneth', 'Jason', or 'Ben'. I can't say for sure because dictionaries are randomly ordered.
Melissa Castle
15,560 PointsFinally got it. Thanks!
Scott Simontacchi
2,071 PointsHi! I'm also having trouble understanding why my code outputs a different name each time; I get that it must have something to do with the random ordering of dictionaries, but don't see it yet. Any ideas? Here's the code:
def most_classes(t_dict):
max_count = 0
for name in t_dict:
count = len(t_dict[name])
if count > max_count:
teacher = name
return teacher
Kenneth Love
Treehouse Guest TeacherYou're never updating max_count so every count is greater than it.
Scott Simontacchi
2,071 PointsWhoops!! Thank you so much!
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherYou aren't returning
teach_namein the end (outside of any for loops or ifs).Aside from that, though, let's look at your second for loop.
classesis going to be each class in the teacher's list of classes. You're then checking to see if the length of a class is longer than the count. This is comparing the length of the name of the class instead of the number of classes a teacher has. Get rid of that for loop and checklen(teach_dict[key])to check the number of classes.