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 trial<noob />
17,062 Pointshelp in the seocnd stage in this challange
Im struggling to solve this challange: the code:
def num_courses(dict):
for x in dict.values():
print(x)
return len(x)
i tried to see what i get when i run it at workspace: https://gyazo.com/26d460b8724d7d70d1941c91f4b83b1d
I tried evreything i know it, I dont want to get the correct answer just to give me hints where im wrong
# 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.
#create a function that take a dictionary and return the length of the dict
def num_teachers(dict):
return len(dict)
def num_courses(dict):
for x in dict.values():
print(x)
return len(x)
5 Answers
Steven Parker
231,275 PointsYou don't need to "print" anything for the challenge, at best it will be ignored but in some challenges it can cause trouble.
But assuming it's just ignored here, your method will loop through the dictionary, and finally return the number of courses associated with the last teacher. But the challenge wants "the total number of courses for all of the teachers". So you'll need to build up a total count to return.
<noob />
17,062 PointsWhy it will only return the number of courses only for the last teacher if i loop trough all the values in the dict?
Steven Parker
231,275 PointsBecause nothing is being done with each value of "x" inside the loop (remember "print" doesn't count). So when the loop is over, "x" still contains the last set of values.
<noob />
17,062 PointsIgnore the print i do it to see what im getting from the output. i understand what your saying but i cant think of a way which i can do what u suggested, how i can add a counter to that?
def num_courses(dict):
total = 0
for x in dict.values():
total += 1
return total
this is what i tried, in each iteration i add 1 to the total, i still get 2 .
Steven Parker
231,275 PointsTry adding the number of courses ("len(x)
") to the total instead of just 1 each time.
<noob />
17,062 Pointsur suggestion is working but i want to understand what happend "under the hood" lets say my dict is :
course_minutes = {"python basics" : 232, "django basics" : 300 "flashbasics" : 189, "javabasics" : 133, 150,160}
the values im iterating over are : 232, 300,189,133 if i dont do nothing inside the for loop and only return the len of (x) the output will be 3 because the for loop is ended and the last iteration gave me 3 so this is what i get. but if i add the len(x) in evrey iteration then "i do something" in the for loop and because of that i will get each iteration the total count?
Steven Parker
231,275 PointsThis example does not conform to the course conditions. The dictionary this challenge is intended to work with will have a teacher's name for every key, and a list of courses for the associated value. See the sample shown in the comments of the original question code.
<noob />
17,062 PointsI change my question then if i does nothing inside the for loop, only return the len of (x) i will get only the length of the last item that i iterated on ? and if i add the len(x) in evrey iteration to total i will get the total length of each x because now im doing something inside the for loop for evrey iteration?
is it the behavior? BTW, I really appreciate u keep answer my question after i asked alot of them :]
Steven Parker
231,275 PointsNow, the code is adding to "total" inside the loop. And inside the loop, "len(x)" is the length of each item, not just the last.
It's only the last outside the loop.