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 trialmamadou diene
Python Web Development Techdegree Student 1,971 PointsThat one wasn't too bad, right? Let's try something a bit more challenging. Create a new function named num_courses tha
i don't know where i am going wrong on this one
# 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(teachers):
teacher_count = 0
for teacher in teachers:
teacher_count +=1
return teacher_count
def num_courses(courses):
total_courses = 0
for course in courses.values():
total_courses +=1
return total_courses
1 Answer
Pete P
7,613 PointsWithin the courses dictionary there are keys and values. Each value in the courses dictionary happens to be a list. The code you currently have, is counting the number of lists in the dictionary rather than the items in each list.
For example, the dictionary given in the comments at the top of the challenge has 2 values (lists): ['jQuery Basics', 'Node.js Basics'] and ['Python Basics', 'Python Collections']. Your code would therefore return total_courses count of 2.
Instead of:
for course in courses.values():
total_courses +=1
return total_courses
Think of it like:
for each_list in courses.values():
# iterate through each list and update total_courses count
return total_courses
You could add a second 'for loop' nested inside of your first to iterate through each list and update total_courses.
Hope this helps. Let me know if you're still having trouble.