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 trialtyler borg
9,429 Pointsteacher stats task 4 of 5...getting frustrated
I've been stuck on this challenge for 2 days and I'm getting really frustrated now...I need to understand what to do. Please help. This is as far as I've gotten:
max_courses = 0
for teacher in teachers:
if len(teachers[teacher]) > max_courses:
max_courses += len(teachers[teacher])
and I could also use the 'value in values' loop method, but can't figure out how to return the name of the teacher with the most courses.
# 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(dict):
teachers = len(dict)
return teachers
def num_courses(dict):
courses = 0
for value in dict.values():
courses += len(value)
return courses
def courses(dict):
courses_list = []
for value in dict.values():
courses_list.extend(value)
return courses_list
def most_courses(dict):
max_courses = 0
for teacher in teachers:
if len(teachers[teacher]) > max_courses:
max_courses += len(teachers[teacher])
3 Answers
Moosa Bonomali
6,297 PointsDo not despair , you are almost there. You need to track the both the teacher's name and the number of courses at the same time. Here is how I would implement it;
def most_courses(dict):
max_courses = 0
instructor = ""
for teacher in teachers:
courses = len(teachers[teacher]) #get the number of courses
if courses > max_courses: #compare with the previous max
max_courses = len(teachers[teacher]) #save the new most courses
instructor = teacher #save the teacher with the most courses
return instructor
horus93
4,333 PointsI've been wrestling with this one or a couple days myself, and the most common problem I ran into was just getting a return of either the key with the higher alphabet value, or the key whose first value had a higher alphabet (or number, value, basically just the first character.), and everywhere I looked online I couldn't find much in the way of any good examples for counting multiple non integer items within dictionaries (the most common consensus was "why the hell would you wanna assign more than one value to any dictionary key? DON'T DO THAT!") so that was less than helpful.
Thankfully you and Moosa helped shed light on this problem for me, as I was starting to go a little insane trying to think of solutions.
One thing I noticed here though is that both of you leave "teachers" undefined in the most courses variable, and from my understanding and limited experience that's going to break the function since it can't pull it from your num_teachers(dict) function. I tested it out to see if maybe the shorter path i'd taken on the first part of the challenge was tripping me up here, but didn't seem to work from my end. (unless that part is intentional keep part of the answer hidden from the eyes of eager / frustrated learners, though anyone at this point should be capable of identifying and fixing that right away.
Moosa Bonomali
6,297 PointsYou are absolutely correct Horus and the answer should have been structured as follows;
def most_courses(dict):
max_courses = 0
for teacher in dict:
if len(dict[teacher]) > max_courses:
max_courses += len(dict[teacher])
horus93
4,333 PointsYea, problem for me is that if i can't see results in some sort of output the understanding doesn't click as well, so the second one you posted here doesn't work well for me because if you run it the output is juts None for me once I rename the variables, and I wasn't sure what to have it return with just that since the results didn't show me anything if I print the function.
Instead I just made a minor tweak to your upper one by adding teachers as a variable inside the function to get it working for me.
def most_courses(tt):
max_courses = 0
instructor = ""
teachers = tt
for teacher in teachers:
courses = len(teachers[teacher])
if courses > max_courses:
max_courses = len(teachers[teacher])
instructor = teacher
return instructor
tyler borg
9,429 Pointstyler borg
9,429 PointsThank you... That make sense how you did it, but can you explain why the empty quotes for the instructor variable? Also, are you sure at the end just putting 'teacher' will get back the keyword of the dict? This is why I couldn't figure out how to use the 'value in values' method, because when you use that you can only access values, not keys, right?