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 trialKudakwashe Chatikobo
1,010 Pointsam failing to get the right number of courses
.
# 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):
return int(len(teachers))
def num_courses(total):
for num in total.values():
return len(num)
3 Answers
<noob />
17,062 PointsHi i was stuck in the exact same challnage, ur getting only the last value because u loop through the values but u donโt do nothing with each iteration and u end up returning the last value. i need to declare a counter and add to that count the len.
<noob />
17,062 PointsChris Freeman itโs not true, even if he use the return statement in the right spot in the end of the for loop it will not work
Chris Freeman
Treehouse Moderator 68,441 PointsSorry, I was not commenting on the position of the return statement. I was commenting on your assertion that โur getting only the last value because u loop through the values but u donโt do nothing with each iteration and u end up returning the last value. As is, the loop will execute a single time and return the length of the first value in total.values()
.
<noob />
17,062 PointsChris Freeman i didnโt knew that, only the len of the first value?
Chris Freeman
Treehouse Moderator 68,441 Points# amending num_courses to print num
>>> def num_courses(total):
... for num in total.values():
... print(num)
... return len(num)
...
# defining mock data
>>> d = {'a': [1],
... 'b': ['2a', '2b'],
... 'c': ['3a', '3b', '3c'],
... 'd': ['4a', '4b', '4c', '4d'],
... }
>>> num_courses(d)
[1]
1
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsSince the
return
statement is indented inside of thefor
loop, it's the first iteration value that is returned.