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 trialJack Cummins
17,417 PointsPlease help me!
I have no clue how to get this to work
Create a function named most_courses that takes our good ol' teacher dictionary. most_courses should return the name of the teacher with the most courses. You might need to hold onto some sort of max count variable.
# 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(test2):
return len(test2)
def num_courses(teachers):
total = 0
for value in teachers.values():
# for the number of courses within that value
for course in value:
total += 1
return total
def courses(teachers):
output = []
for courses in teachers.values():
output +=courses
def most_courses(dic):
course_count = 0
teacher_name = ''
for teacher, courses in dic.items():
if len(courses) > course_count:
teacher_name = teacher
return teacher_name
return output
2 Answers
andren
28,558 PointsHis code does indeed pass, but technically speaking it shouldn't. As it actually does not accomplish what the task is asking for.
He never assigns anything besides 0 to course_count
so his comparison of len(courses) > course_count
will always evaluate to True
, assuming all teachers has at least 1 course.
It seems the challenge always passes you a dictionary where the last teacher has the largest course, so as long as you return the last teacher in the dictionary your code will pass, even if you don't do any size comparison at all.
This code for example passes just fine (I tested it multiple times):
def most_courses(dic):
teacher_name = ''
for teacher, courses in dic.items():
teacher_name = teacher
return teacher_name
Which seems like a bit of a code checker bug to me. The dictionary should be randomized so you actually can't pass without doing some size checking like the task is asking you to do.
Jennifer Nordell
Treehouse TeacherHi there, Jack Cummins! Would it comfort you to know that the function works just fine? Now, I'm not sure how this happened exactly but somehow you started that new function before the return
statement of the courses
function above it.
Your last line is return output
, but that should be up in the courses
function. When I move that up to its proper place, your code passes through step 4!
Hope this helps!
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi andren ! Hope you don't mind, terribly, but I changed your comment to an answer. You're correct, and I didn't even spot it. But no, he never reassigns the value of
count
. So, it will always be the last teacher looked at. Thanks for helping out in the Community! Also, I'm submitting a bug report to Support.