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 trialRodrigue Loredon
1,338 PointsI can't figure out how to write the nested for loops to get a count of the teachers's courses in the teachers.py script
Hi, I've been on this for 24 hours and still can't figure it out.
# The dictionary will be something like:
# 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.
teachers = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
'Kenneth Love': ['Python Basics', 'Python Collections']}
def most_classes(teachers):
max_count = 0
course_number = 0
teacher_with_most_classes =''
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsHi Rodrigue, You don't necessarily need nested loops (a loop within a loop) for this task.
A bit of background: A dictionary can be iterated over in several ways using a for
loop as shown in the ipython session below.
# define a dictionary
In [24]: d1 = {'a': 1, 'b': 2}
# list keys of dictionary
In [25]: d1.keys()
Out[25]: dict_keys(['a', 'b'])
# use keys() to create iterable list of keys
In [26]: for key in d1.keys():
print(key)
....:
a
b
# list values of dictionary
In [29]: d1.values()
Out[29]: dict_values([1, 2])
# use values() to create iterable list of values
In [30]: for value in d1.values():
print(value)
....:
1
2
# list key value pairs of dictionary as Tuples
In [27]: d1.items()
Out[27]: dict_items([('a', 1), ('b', 2)])
# use items() to create iterable list of key value tuples
In [28]: for item in d1.items():
print(item)
....:
('a', 1)
('b', 2)
# list key value pairs of dictionary as Individual items
In [31]: d1.items()
Out[31]: dict_items([('a', 1), ('b', 2)])
# use items() to create iterable list of key value individually
In [32]: for key, value in d1.items():
....: print(key, value)
....:
a 1
b 2
# The default assumption is to iterate over the keys:
In [33]: for key in d1:
....: print(key)
....:
a
b
Using the default method, you can iterate over the dictionary keys like this:
def most_classes(teachers):
max_count = 0
course_number = 0
teacher_with_most_classes =''
# loop over each key in teachers dictionary
for teacher in teachers:
# check if the length of this teacher's class list is a new max
if len(teachers[teacher]) > max_count:
# save this teacher as new max
max_count = len(teachers[teacher])
teacher_with_most_classes = teacher
# return max teacher name
return teacher_with_most_classes
[**Edit**: added missing max_count assignment --cf]
Rodrigue Loredon
1,338 PointsHello Chris, I appreciate your help thank you but I wasn't able to pass that challenge. I even tried copy pasting your code but the script still doesn't return the right teacher. Take care
Chris Freeman
Treehouse Moderator 68,441 PointsOops! I forgot to update max_count. See corrected answer above.
Rodrigue Loredon
1,338 PointsAwsome! It works and helped my understanding a lot. Thank you VERY VERY MUCH :)