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 trialMałgorzata Staniszewska
2,431 PointsPlease please help with dicts!:(
Task one is no longer passing. Why? What is wrong with my code in general?
# The dictionary will be something like:
# {'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(dictionary):
max_count = 1
for name in dictionary:
count = len(dictionary[name])
if count > max_count:
max_count = count
if len(dictionary[name]) == max_count:
return(name)
def num_teachers(dictionary):
list_t = []
for name in dictionary:
list_t.append(name)
return(len(list_t))
def stats(dictionary):
list_of_lists = []
for name in dictionary:
nr = len(dictionary[name])
list_of_lists.append(name,nr)
return(list_of_lists)
1 Answer
jacinator
11,936 PointsYour function was just returning a random teacher. It passed the first time, but it couldn't be counted on. Try this code. It should work.
def most_classes(dictionary):
most_classes_teacher = None
most_classes_count = 0
for teacher in dictionary:
if len(dictionary[teacher]) >= most_classes_count:
most_classes_teacher = teacher
most_classes_count = len(dictionary[teacher])
return most_classes_teacher
def num_teachers(dictionary):
return len(dictionary.keys())
def stats(dictionary):
teacher_count_list = []
for teacher in dictionary:
teacher_count_list.append([teacher, len(dictionary[teacher])])
return teacher_count_list
Małgorzata Staniszewska
2,431 PointsMałgorzata Staniszewska
2,431 PointsThx a lot!:)