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 trialColin Gaul
4,942 PointsHaving trouble
I have the first 3 steps down but I can't seem to figure out the last one. Getting "You returned 5 courses, you should have returned 18." Here is my code: EDIT: It is also saying that task 1 is no longer passing when I try to run the code.
def most_classes(teachers):
counts = []
max_classes = 0
for key in teachers:
counts.append(len(teachers[key]))
for value in counts:
if value > max_classes:
max_classes = value
for key in teachers:
if max_classes == len(teachers[key]):
return key
def num_teachers(teach_dict):
teach_count = 0
for name in teach_dict:
teach_count += 1
return teach_count
def stats(teachers):
total_stats = []
for teacher in teachers:
total_stats.append( [teacher, len(teachers[teacher])] )
return total_stats
def courses(teachers):
return teachers
3 Answers
Kenneth Love
Treehouse Guest TeacherColin Gaul The Shopping List stage of Python Basics and the list and dictionary parts of Python Collections should be helpful. I'm sure you can get this already, though. Let me start you off.
def courses(teachers_dict):
output = []
# loop over all of the .values() in teachers_dict and add their contents into output
return output
Kenneth Love
Treehouse Guest Teachercourses()
should return a list of all of the courses, not the teachers, as your function is doing. Each key in the dict is a teacher and the value for a teacher is a list of their courses. So combine all of the values into one list and send that back.
Colin Gaul
4,942 PointsSo it would be like courses(**teach_dict), then return the values of them?
Kenneth Love
Treehouse Guest TeacherColin Gaul well, not exactly. We don't want the dictionary's keys as named arguments. We want to look through all of the keys in the dictionary and combine their values into one big, flat list. And then return that list. This is a good challenge to use the dict.values()
method.
Colin Gaul
4,942 PointsSo something like: for value in teach_dict.values() then return(value + key)?
Kenneth Love
Treehouse Guest TeacherThat would definitely be part of the solution, yes.
Colin Gaul
4,942 PointsStill not understanding, is there a video where I can watch again to help?
Colin Gaul
4,942 PointsGot it Kenneth! Thanks for all your help!
Kenneth Love
Treehouse Guest TeacherAwesome! Glad you stuck it out and got through
Colin Gaul
4,942 PointsColin Gaul
4,942 PointsI get the looping part, it's just adding the values to output is getting me.
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherColin Gaul we went over different ways to add lists together or put the content of one list into another list in both Python Basics and Python Collections. I'll suggest watching the first two videos here
Colin Gaul
4,942 PointsColin Gaul
4,942 PointsWhen I try testing my code, it's asking "where's courses()?"
I think you would use .extend() right? To add on more things to the list?
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherColin Gaul yes, you'll want to use
.extend()
. As for the "Where'scourses()
?", can you show us your code so we can help debug that?Colin Gaul
4,942 PointsColin Gaul
4,942 PointsSure:
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherColin Gaul well, looking at your script above, your function takes
teachers_dict
but you're usingteach_dict
in the function. If you make those two match up, it should work better.Also, you want to extend with
values
, notteach_dict.values()
, since you want the current set of values.