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 trialAndrew McLane
3,385 PointsReturning the name of teacher with the most courses.
Not sure what the hint in the challenge means when it says hold onto some sort of max count variable. Is there a max count method built into python?
# 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(single_arg):
return len(single_arg)
def num_courses(single_arg):
my_list = []
my_value = single_arg.values()
for value in my_value:
my_list.extend(value)
return len(my_list)
def courses(single_arg):
my_list = []
my_value = single_arg.values()
for value in my_value:
my_list.extend(value)
return my_list
def most_courses(single_arg):
count = 0
1 Answer
Manish Giri
16,266 PointsThe hint indicates to keep a variable that will hold the current count for the number of courses taught by the teacher, as you iterate through the list. If you come across a teacher who has more courses than the value in the current variable, you change the variable to hold this teacher's name.
Consider this example -
maxItem = 0
for num in [10, 2, 4, 1, 40]:
if num > maxItem:
maxItem = num
Here, you start by iterating through the list. You check if the current element is greater than the value stored in the maxItem
variable, if so - you update it.
At the end, maxItem
will hold the value of the largest element in the list.