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 trialJulian Silvestri
4,214 PointsTeacher Stats Challenge.... im so confused
I am trying to do the first challenge of the 5 required... cant seem to pass it... Not sure what I am missing...
please help
# 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(arg1):
number_of_keys = 0
for x in arg1.keys():
number_of_keys += 1
return number_keys # <- [MOD: should be number_of_keys - srh]
3 Answers
Steve Hunter
57,712 PointsHi Julian,
If you access the dictionary using its .keys()
method, you can pass that into a len()
method and return the result. No need for a loop.
Something like:
def num_teachers(dict):
return len(dict.keys())
I hope that makes sense.
Steve.
Julian Silvestri
4,214 Pointsthank you !
Steve Hunter
57,712 PointsNo problem! This set of challenges is tough!!
Herman Brummer
6,414 Pointsdef num_teachers(dictionary_list):
# number of teachers
return len(dictionary_list)
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsIncidentally, your code is fine but you returned a variable of a different name. I'll add a comment in the code in your question.
This is the same as your code and works fine.