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 trialThomas Katalenas
11,033 Pointsgive me a hint!?
# going the wrong way with this the only way this would work is if i make the teachers list append sub lists or tupples.
# or some dictionary stuff which is probably the correct answer.
def most_classes(dict_teachers)
teachers = []
max_count = count
teachers.append(teacher)
teachers.append(max_count)
count = 0
for key in dict_teachers:
count += 1
teacher = key
print(dict[key])
2 Answers
qlpxjevhuv
10,503 PointsHi Thomas,
The challenge specifies a dictionary for you. You have to define a function called 'most_classes' which takes a dictionary as a formal parameter and returns the name of the teacher with the most classes.
def most_classes(our_dict):
...do things...
return teacher
Next, we need two variables to store 1) the name of the teacher with the most classes and 2) the number of classes they have.
def most_classes(our_dict):
teacher = ''
most_classes = 0
return teacher
We need to loop through the teachers in the dictionary and check the number of classes they have, so we need a for loop.
def most_classes(our_dict):
teacher = ''
most_classes = 0
for teacher in our_dict:
num_classes = len(our_dict[teacher]) # Pulls out teacher's values and stores it as an int.
if num_classes > most_classes:
mostClasses = num_classes
teacher = teacher
return teacher
Thomas Katalenas
11,033 PointsHere is my updated code I thought max(our_dict) would be nice and optimized, returns right for the example dictionary teacher classes. So this works correctly
def most_classes(our_dict):
count = 0
max_count = 0
teacher = max(our_dict)
for i in our_dict:
count = len(our_dict[i])
if count > max_count:
max_count = count
if teacher == i:
return teacher
else:
return i
hmm I wonder why this doesn't work because max() would be faster more speedy I think... anyway thats my theory.
def most_classes(our_dict): max_classes = max(our_dict) return max_classes