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 trialRonald Tse
5,798 PointsI can run it and get the correct ans...
for the last part, I have tried to run it I get the correct ans. But it said I'm wrong...
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
# 'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
def num_teachers(argument):
teacher_list = []
for teacher in argument:
teacher_list.append(teacher)
return len(teacher_list)
def num_courses(argument):
course_list = []
for course in argument.values():
course_list += course
return len(course_list)
def courses(argument):
course_list2 = []
for course in argument.values():
course_list2 += course
return course_list2
def most_courses(argument):
max_course = 0
for teacher in argument.keys():
if len(argument[teacher]) > max_course:
max_course = len(argument[teacher])
return teacher
1 Answer
Steven Parker
231,248 PointsYou're very close, but missing one thing.
When you encounter a new maximum and save the number of courses for later comparisons, you should also save the teacher that number goes with. Then, after the loop finishes, return that saved teacher instead of just the last one tested in the loop.
Perhaps it appeared to work because you tested it with a list where the last teacher also happened to be the one with the most courses. Note that example data contained in challenge comments will almost never be the actual data used for testing.