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 trialpet
10,908 PointsIn challenge Task 2:5 I'm getting "Bummer: Didn't get the right number of courses!"
Here's my code:
# 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(tec_dict):
tecs = 0
for item in tec_dict.keys():
tecs += 1
return tecs
def num_courses(tec_dict):
courses = 0
for value in tec_dict.values():
courses += 1
return courses
3 Answers
Alexander Davison
65,469 PointsEvery teacher has a certain number of courses. Not all of them have one course. So why are you adding 1
to courses
for every value?
Try adding len(value)
to courses
every iteration instead.
I hope this helps!
Happy coding! ~Alex
jorgelpezguzmn
6,830 PointsI am not sure if you want to get the overall number of teachers and courses. But in that case, here is the code:
def num_teachers(tec_dict):
return len(tec_dict.keys())
def num_courses(tec_dict):
courses = 0
for value in tec_dict.values():
courses += len(value)
return courses
I hope this helps too!
nakalkucing
12,964 PointsHey jorgelpezguzmn, I know you just wanted to help. But just so you know, Treeehouse asks that we do not simply give away the code. They request that when answering a question you use hints like Alex did above. :) Happy coding, Nakal
jorgelpezguzmn
6,830 PointsI'm sorry. I did not know that. Thanks for the advice!
nakalkucing
12,964 PointsNo problem! :)
pet
10,908 PointsThanks for all the help.