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 trialJordan Pierre
16,601 PointsQuestion about why my code won't work for the Teacher Stats code challenge
the task asks me to 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.
This is my code:
def most_courses(teachers_list):
max_value = 0
for value in teachers_list.values():
if len(value) > max_value:
max_value = value
print(max_value)
most_courses({'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
'Kenneth Love': ['Python Basics', 'Python Collections', 'Flask Basics']})
My question is, why won't the greater than operator compare the value to the max value?
1 Answer
Dave Harker
Courses Plus Student 15,510 PointsHi Jordan Pierre,
You're trying to assign a list into a variable you've defined as an int.
max_value = 0 # int
for value in teachers_list.values():
if len(value) > max_value:
max_value = value # assigning list into int **change this**
Change that to:
max_value = len(value) # done
Have fun. Side note: That code won't actually complete the challenge requirements by the way. If that is the 4th challenge you'll need to return which teacher has the most courses, not how many they have. It's only an extra line of code though so based on what you've done so far I'm sure you'll have no trouble returning that value (not printing it).
Keep up the good work, and happy coding.
Dave
Dave Harker
Courses Plus Student 15,510 PointsDave Harker
Courses Plus Student 15,510 PointsHint: use teachers_list.items() so you can keep track of the busiest teacher also