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 trialJade Jupiter
2,224 PointsWhy isn't my code working
This question is from Teacher Stats task 4 out of 4 from Python Collections I keep on getting the error 'Bummer! You returned 5 courses, you should have returned 18 can someone help me?
The dictionary will be something like:
teachers = {'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(teachers): max_count = 0; teacher = "" for key in teachers: count = len(teachers[key]) if count > max_count: teacher = key max_count = count return teacher
def num_teachers(teachers): count = 0 for key in teachers: count += 1 return count
def stats(teachers): stats_list = [] teacher = "" for key in teachers: teacher = key count = len(teachers[key]) stats_list.extend([[teacher, count]]) return stats_list
def courses(teachers): courses = [] for value in teachers.values(): courses.append(value) return courses
2 Answers
Steven Parker
231,110 PointsI think I've spotted your problem despite the formatting issue...
It looks like you're collecting lists instead of individual courses.
The dictionary values are lists of courses, so if you append a value you're adding the entire list as a single item.
To add the items, use extend rather than append, or concatenate the list with +=
.
Aby Abraham
Courses Plus Student 12,531 Pointsdef courses(dict):
clist = []
for value in dict.values():
clist.extend(value)
return clist
Steven Parker
231,110 PointsSteven Parker
231,110 PointsIt's VERY hard to read Python without proper code formatting.
See the Markdown Cheatsheet pop-up below the answer area for code formatting info.