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 trialBrandon Hoffman
3,642 PointsTeacher.PY Confused on why getting an error
QUESTION: "Create a new function named num_courses that will receive the same dictionary as its only argument. The function should return the total number of courses for all of the teachers." Just watched the previous video about this and this is an exact scenario that he went through almost and for some reason it isn't getting me past part 2 of the challenge.
# 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.
teacher_dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
'Kenneth Love': ['Python Basics', 'Python Collections']}
def num_teachers(teacher_dict):
count = 0
for teacher in teacher_dict.keys():
count +=1
return count
def num_courses(teacher_dict):
count = 0
for value in teacher_dict.values():
count +=1
return count
1 Answer
Omar Farag
4,573 PointsIn your code, you're just adding 1 every single time a key has a value, and not actually counting how many items there are in that value. Instead of adding one to the count variable, add the length of the value. For example:
def num_courses(teacher_dict):
count = 0
for value in teacher_dict.values():
count += len(value)
return count